Skip to content

Instantly share code, notes, and snippets.

@brandonkelly
Created May 18, 2012 06:31
Show Gist options
  • Save brandonkelly/2723569 to your computer and use it in GitHub Desktop.
Save brandonkelly/2723569 to your computer and use it in GitHub Desktop.
Remove <script> tags from HTML
// 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