labels: php , css , html , dom , DomDocument
/**
* Remove html dom element by class name
*
* @param string $className
* @param string $html
* @return string
*/
function remove_dom($className, $html)
{
$dom = new DomDocument();
$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'), LIBXML_NOWARNING | LIBXML_NOERROR);
$finder = new DomXPath($dom);
$nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $className ')]");
if ($nodes->count() == 0) {
return $html;
}
$nodes->item(0)->parentNode->removeChild($nodes->item(0));
return $dom->saveHTML();
}