Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
21
rated 0 times [  28] [ 7]  / answers: 1 / hits: 54958  / 15 Years ago, fri, december 11, 2009, 12:00:00

I am trying to remove JavaScript from the HTML.



I can't get the regular expression to work with PHP; it's giving me an null array. Why?



<?php
$var = '
<script type=text/javascript>
function selectCode(a)
{
var e = a.parentNode.parentNode.getElementsByTagName(PRE)[0];
if (window.getSelection)
{
var s = window.getSelection();
if (s.setBaseAndExtent)
{
s.setBaseAndExtent(e, 0, e, e.innerText.length - 1);
}
else
{
var r = document.createRange();
r.selectNodeContents(e);
s.removeAllRanges();
s.addRange(r);
}
}
else if (document.getSelection)
{
var s = document.getSelection();
var r = document.createRange();
r.selectNodeContents(e);
s.removeAllRanges();
s.addRange(r);
}
else if (document.selection)
{
var r = document.body.createTextRange();
r.moveToElementText(e);
r.select();
}
}
</script>
';

function remove_javascript($java){
echo preg_replace('/<scriptb[^>]*>(.*?)</script>/i', , $java);

}
?>

More From » php

 Answers
9

this should do it:



echo preg_replace('/<scriptb[^>]*>(.*?)</script>/is', , $var);


/s is so that the dot . matches newlines too.



Just a warning, you should not use this type of regexp to sanitize user input for a website. There is just too many ways to get around it. For sanitizing use something like the http://htmlpurifier.org/ library


[#98094] Tuesday, December 8, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zackeryzainv

Total Points: 61
Total Questions: 102
Total Answers: 99

Location: Andorra
Member since Sat, May 27, 2023
1 Year ago
;