March 31, 2012
preg_replace script, link tag not working
Question by john
I used the following code to remove script, link tags from my string,
$contents='<script>inside tag</script>hfgkdhgjh<script>inside 2</script>';
$ss=preg_replace('#<script(.*?)>(.*?)</script>#is', '', $contents);
echo htmlspecialchars($ss);
it works fine. But can I use anything that similar to html parsing rather than preg_match for this?
Answer by Starx
Here are few things you can do
htmlspecialchars()
can prove those tags uselessstriptags()
removes all HTML tags
But the technique you are using is the correct one. However here is a improved version for that
echo preg_replace('/<scriptb[^>]*>(.*?)</script>/is', "", $contents);