- str_replace: documentation ( source)
- preg_replace: documentation ( source)
<?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";
}