Probably the simplest method would be str_ireplace()
for case-insensitive replacement, however this won't preserve the case of the "sCriPt" word. But if you're out to de-fang XSS attacks that may be just fine:
str_ireplace("<script>", "<script>", $input);
A more complex solution could be devised with preg_replace()
to preserve case, but would be slower. This might work, but if it were me I'd use str_ireplace()
...
preg_replace("/<(script)>/i", "<$1>", $input);
Note: If it is XSS prevention you're after, neither of these takes into account things like <script type=text/javascript>
. To truly handle these cases, you need to load the HTML string into DOMDocument
and delete the offending script nodes.