I need to remove all instances of a particular class (only assigned to div
s) 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?