- libxml_use_internal_errors: documentation ( source)
<?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));