DOMDocument remove script tags from HTML source

后端 未结 2 386
后悔当初
后悔当初 2021-01-11 11:03

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

2条回答
  •  花落未央
    2021-01-11 11:21

    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);
    };  
    

提交回复
热议问题