fork download
  1. <?php
  2.  
  3. $html_src = "
  4. <!DOCTYPE html>
  5. <html>
  6. <body>
  7. <a href=\"annimon.com\">goto home</a>
  8. <script>
  9. alert(\"Victor Protector doesn't work\");
  10. </script>
  11. <br>
  12. <b>&copy; aNNiMON</b>
  13. <script>
  14. alert(\"Victor Protector doesn't work твайс\");
  15. </script>
  16. </body>
  17. </html>
  18. ";
  19.  
  20. echo "source is: " . $html_src;
  21.  
  22.  
  23. $d = new DOMDocument;
  24. $d->loadHTML($html_src);
  25. $elements = $d->getElementsByTagName("script");
  26. for ($i = $elements->length; --$i >= 0; ) {
  27. $href = $elements->item($i);
  28. $href->parentNode->removeChild($href);
  29. }
  30. $d->formatOutput = true; // красивый вывод
  31. $html_clean = $d->saveHTML();
  32.  
  33. echo "\n\n filtered is:\n " . $html_clean;
Success #stdin #stdout 0.01s 24452KB
stdin
Standard input is empty
stdout
source is: 
<!DOCTYPE html>
<html>
	<body>
		<a href="annimon.com">goto home</a>
		<script>
			alert("Victor Protector doesn't work");
		</script>
		<br>
		<b>&copy; aNNiMON</b>
		<script>
			alert("Victor Protector doesn't work твайс");
		</script>
	</body>
</html>


 filtered is:
 <!DOCTYPE html>
<html><body>
		<a href="annimon.com">goto home</a>
		<br><b>&copy; aNNiMON</b>
		</body></html>