3

I need to remove all instances of a particular class (only assigned to divs) from an HTML document. Here's the snippet I'm using:

$dom = new DOMDocument;
    $dom->loadHTML($meaning);
foreach ($dom->getElementsByTagName('div') as $node) {
        if($node->getAttribute('class') == "someclass"){
            $node->parentNode->removeChild($node);
        }
    }

This works fine except that it only removes the first occurrence of the said class, leaving the rest behind. Is this normal behavior? What should I modify in my code to ensure all nodes attributed to this class are removed?

TheLearner
  • 2,813
  • 5
  • 46
  • 94

1 Answers1

3

Give this a shot. The key is to use DomNodeList::item(), as you'll see below:

$dom = new DOMDocument();
$dom->loadHTML($meaning);

/** @var DomNodeList $divs */
$divs = $dom->getElementsByTagName('div');

for ($i = 0; $i < $divs->length; $i++) {
    /** @var DomElement $node */
    $node = $divs->item($i);
    if ($node->getAttribute('class') == 'someclass') {
        $node->parentNode->removeChild($node);
        $i--; // Since we just removed an element, we need to readjust our pointer.
    }
}
Nate
  • 1,442
  • 14
  • 22