I used @Alex\'s approach here to remove script tags from a HTML document using the built in DOMDocument. The problem is if I have a script tag with Javascript content and th
To avoid that you get the surprises of a live node list -- that gets shorter as you delete nodes -- you could work with a copy into an array using iterator_to_array:
foreach(iterator_to_array($dom->getElementsByTagName($tag)) as $node) {
$node->parentNode->removeChild($node);
};