Last active
August 29, 2015 14:15
-
-
Save shenjunru/d2cf79b60ba9b45379be to your computer and use it in GitHub Desktop.
strip script blocks form 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
function strip_script(content, scripts, fix){ | |
var so = 0, ss = 0, se, cs, ce; | |
var save = Object(scripts) instanceof Array; | |
while (-1 !== ( ss = content.indexOf('<script', ss) )) { | |
// update comment range, if it is before the script | |
while (null == ce || -1 !== ce && ss > ce) { | |
if (-1 !== ( cs = content.indexOf('<!--', null == ce ? so : ce) )) { | |
if (-1 === ( ce = content.indexOf('-->', cs) )) { | |
// fix comment end tag | |
if (true === fix) { | |
content += '-->'; | |
} | |
ce = content.length; | |
} | |
} else { | |
ce = -1; | |
} | |
} | |
// skip the script inside the comment | |
if (-1 !== ce && cs < ss && ss < ce) { | |
so = ss += 1; | |
continue; | |
} | |
// strip script | |
se = content.indexOf('</script>', ss); | |
if (-1 === se) { | |
// fix script end tag | |
if (true === fix) { | |
content += '</script>'; | |
} | |
se = content.length; | |
} else { | |
se += 9; | |
} | |
if (save) { | |
scripts.push(content.substr(ss, se - ss)); | |
} | |
content = content.substr(0, ss) + content.substr(se); | |
so = ss; | |
ce = null; | |
} | |
return content; | |
} |
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
<?php | |
function strip_script($content='', &$scripts=null, $fix=false){ | |
$ss = 0; $so = 0; | |
$save = is_array($scripts); | |
while (FALSE !== ( $ss = stripos($content, '<script', $ss) )) { | |
// update comment range, if it is before the script | |
while (!isset($ce) || FALSE !== $ce && $ss > $ce) { | |
if (FALSE !== ( $cs = strpos($content, '<!--', isset($ce) ? $ce : $so) )) { | |
if (FALSE === ( $ce = strpos($content, '-->', $cs) )) { | |
// fix comment end tag | |
if (TRUE === $fix) { | |
$content.= '-->'; | |
} | |
$ce = strlen($content); | |
} | |
} else { | |
$ce = FALSE; | |
} | |
} | |
// skip the script inside the comment | |
if (FALSE !== $ce && $cs < $ss && $ss < $ce) { | |
$so = $ss += 1; | |
continue; | |
} | |
// strip script | |
$se = stripos($content, '</script>', $ss); | |
if (FALSE === $se) { | |
// fix script end tag | |
if (TRUE === $fix) { | |
$content.= '</script>'; | |
} | |
$se = strlen($content); | |
} else { | |
$se += 9; | |
} | |
if ($save) { | |
$scripts[] = substr($content, $ss, $se - $ss); | |
} | |
$content = substr($content, 0, $ss).substr($content, $se); | |
$so = $ss; | |
unset($ce); | |
} | |
return $content; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment