3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = 'This should be extracted <p>I do not want this</p> This should also be extracted <a>This may appear after other tags and I do not want this</a>'; $doc = new DOMDocument(); $doc->loadHTML("<div>$html</div>", LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED); $xpath = new DOMXPath($doc); $texts = array(); foreach ($xpath->query('/div/text()') as $text) { $texts[] = $text->nodeValue; } print_r($texts);

preferences:
26 ms | 402 KiB | 5 Q