Jump to content

Recommended Posts

Hi

I am looking for regex for taking external Javascript tag (and not inline js) inside array and removing those external javascript entry from html.

Similarly I want to do with inline script but in separate array.

// external js script

<script src="file.js"></script>

 

// inline

<script>

var js= 0;

</script>

 

Now, I want to get script with src attribute in an array and remove those from the code. and similar for inline script.

 

Thank You!

 

Currently I have this code. Whatever I do I could not achieve what I want. something missed everytime.

<?php
ob_start();
?>
HTML code ...
...
..
<?php


///*
$s_header = ob_get_contents();

ob_end_clean();

preg_match_all('/(script|src)=("|\')[^"\'>]+/i', $s_header, $media);

$js = preg_replace('/(src)+("|\'|="|=\')(.*)/i', "$3", $media[0]);


// get inline

//preg_match_all('/<script([^>].*)(^src).*([^>]*)>/i', $s_header, $jsInline);
$reg = '';

function strip_tags_content($text, $tags = '', $invert = FALSE)
{
  global $reg;
  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 {

  $reg = '<('. implode('|', $tags) .')\b.*?>.*?</\1>';
      return preg_replace("@$reg@si", '', $text);
    }
  }
  elseif($invert == FALSE) {
    return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text);
  }
  return $text;
}

$s_header = strip_tags_content($s_header, '<script>', true);

foreach ($js as $jsFile)
{
$allJs .= "<script src='$jsFile'></script>\n";
}

echo $s_header.$allJs;

//echo '<hr>';
//print_r($js);
//print_r($jsInline);
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/190506-regex-for-taking-in-array/
Share on other sites

I may catch one but for other!

Please Remember, I want to catch either of the one (inline or external script) at a time.

 

 

/<script[^>]+?src[^>]*?>\s*(?:<\/script>)?/i

 

I'm sure there is better regex for this.. but the above should work

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.