I need a regex expert for my problem.
I want to get all external script in a variable and not inline script.
Ex:
<script type=“text/javascript” > alert(‘’)</script>
<script > alert(‘’)</script>
<script src=“” type=“text/javascript”></script>
<script src=“” type=“text/javascript” language=“javascript”></script>
I just want to get external script in a variable and inline script in another variable. Is it possible.
I have this code right now:
preg_match_all('/(script|src)=("|\\')[^"\\'>]+/i', $s_header, $media);
$js = preg_replace('/(src)("|\\'|="|=\\')(.*)/i',"$3",$media[0]);
// get inline
preg_match_all('/(\\<script>)/i', $s_header, $jsInline);
function strip_tags_content($text, $tags = '', $invert = FALSE) {
preg_match_all('/<(.+?)[\\s]*\\/?[\\s]*>/si', trim($tags), $tags);
$tags = array_unique($tags[1]);
if(is_array($tags) AND count($tags) > 0) {
if($invert == FALSE) {
return preg_replace('@<(?!(?:'. implode('|', $tags) .')\\b)(\\w+)\\b.*?>.*?</\\1>@si', '', $text);
}
else {
return preg_replace('@<('. implode('|', $tags) .')\\b.*(src)+.*?>.*?</\\1>@si', '', $text);
}
}
elseif($invert == FALSE) {
return preg_replace('@<(\\w+)\\b.*?>.*?</\\1>@si', '', $text);
}
return $text;
}
$data = strip_tags_content($s_header, '<script>', true);
foreach ($js as $jsFile)
{
$allJs .= "<script src='$jsFile'></script>\
";
}
echo $data.$allJs;