<?php
function remove_script_tags($html){
$dom = new DOMDocument();
$dom->loadHTML($html);
$script = $dom->getElementsByTagName('script');
$remove = [];
foreach($script as $item){
$remove[] = $item;
}
foreach ($remove as $item){
$item->parentNode->removeChild($item);
}
$html = $dom->saveHTML();
$html = preg_replace('/<!DOCTYPE.*?<html>.*?<body><p>/ims', '', $html);
$html = str_replace('</p></body></html>', '', $html);
return $html;
}
$s = [];
$s[] = "hey buddy what's up script";
$s[] = "Hi Friend <script>alert('hello world');</script>";
$s[] = "<script src=\"http://jquery.com\">joyjoy</script>happy";
foreach($s as $t){
echo $t.' ==> '.remove_script_tags($t)."\n";
}
- Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.29, 8.2.0 - 8.2.21, 8.3.0 - 8.3.9
- hey buddy what's up script ==> hey buddy what's up script
Hi Friend <script>alert('hello world');</script> ==> Hi Friend
<script src="http://jquery.com">joyjoy</script>happy ==> happy
preferences:
162.37 ms | 403 KiB | 198 Q