3v4l.org

run code in 300+ PHP versions simultaneously
<?php libxml_use_internal_errors(true); $dom = new DOMDocument; $dom->loadHTML('<h1>junk</h1><div>wth?</div>ANYTHING HERE<div class="col"></div>'); $xpath = new DOMXPath($dom); $found = $xpath->query('//div[@class="col"]/preceding-sibling::text()'); if ($found instanceOf DOMNodeList && $found->length > 0) { $text = $found->item(0); $comment = $dom->createComment(''); $text->parentNode->replaceChild($comment, $text); } echo $dom->saveXML($dom->getElementsByTagname('body')->item(0));

preferences:
35.53 ms | 402 KiB | 5 Q