简体   繁体   中英

How to delete element and its children using php's HTMLDOMDocument class

I'm relatively new to php's HTMLDOMDocument class.

I'm doing something like this:

$html = getHTML();

$htmlDOM = new DOMDocument('5.0', 'utf-8');
libxml_use_internal_errors(true);
$htmlDOM->loadHTML(mb_convert_encoding(($html), 'HTML-ENTITIES', 'UTF-8'));
libxml_clear_errors();

Unfortunately all the elements that are retrieved has classes, not IDs. Retrieving elements by Id or Tags are pretty straightforward...

But, how do I retrieve a few elements inside with a specific class (eg: post-hover ) and then delete them from $htmlDOM ?

No idea why they haven't added a getElementsByClassName yet. But you can use xpath to find the elements instead (taken from here ):

$finder = new DomXPath($dom);
$classname = "my-class";
$nodes = $finder->query("//*[contains(@class, '$classname')]");

Then simply loop through and delete:

foreach($nodes as $node){
    $node->parentNode->removeChild($node);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM