Regex for HTML tags with certain attributes

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;

A way you could do it yourself is:
-match all script tags
-loop through the matches, using stripos() to decide if it has an src or not. Use this logic to put each element into appropriate arrays.

Good point.
but I have tried much and could not correct it.
but here you given a ray of hope. I will surely try again.