Created
May 18, 2012 06:31
-
-
Save brandonkelly/2723569 to your computer and use it in GitHub Desktop.
Remove <script> tags from HTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Remove any <script> tags | |
// Go backwards to factor in nested script tags, e.g. <script>document.write('<script></script>')</script> | |
$offset = strlen($html) - 9; // Minimum match length is 9 chars (<script/>) | |
for ($offset; $offset >= 0; $offset--) | |
{ | |
$substr = substr($html, $offset); | |
if (preg_match('/^<script[^>]+?\/>|^<script(?:.|\s)*?\/script>/im', $substr, $match)) | |
{ | |
$html = substr($html, 0, $offset).substr($html, $offset+strlen($match[0])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment