0

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 ?

maxxon15
  • 1,559
  • 4
  • 22
  • 35

1 Answers1

1

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);
}
Community
  • 1
  • 1
rjdown
  • 9,162
  • 3
  • 32
  • 45