3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html_string = " <div> Below are object Node with the html code </div> <script> alert('i want this to be replaced. it has no newline'); </script> <div> I don't want this to be replaced </div> <script> console.log('i also want this to be replaced. It has newline'); </script> <div> This is a div tag and not a script, so it should not be replaced </div> <script> console.warn(Finally, this should be replaced, it also has newline'); </script> <div> The above is the final result of the replacements </div> "; $doc = new DOMDocument(); $doc->loadHTML("<html>$html_string</html>", LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new DOMXPath($doc); foreach ($xpath->query('//script') as $script) { $comment = $doc->createComment('THIS SCRIPT CONTENT HERE HAS BEEN ALTERED'); $script->parentNode->replaceChild($comment, $script); } echo substr($doc->saveHTML(), 6, -8);
Output for 7.1.25 - 7.1.28, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
<div> Below are object Node with the html code </div> <!--THIS SCRIPT CONTENT HERE HAS BEEN ALTERED--> <div> I don't want this to be replaced </div> <!--THIS SCRIPT CONTENT HERE HAS BEEN ALTERED--> <div> This is a div tag and not a script, so it should not be replaced </div> <!--THIS SCRIPT CONTENT HERE HAS BEEN ALTERED--> <div> The above is the final result of the replacements </div>

preferences:
89.48 ms | 408 KiB | 5 Q