satya61229 Posted February 1, 2010 Share Posted February 1, 2010 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/190506-regex-for-taking-in-array/ Share on other sites More sharing options...
RussellReal Posted February 1, 2010 Share Posted February 1, 2010 /<script[^>]+?src[^>]*?>\s*(?:<\/script>)?/i I'm sure there is better regex for this.. but the above should work Quote Link to comment https://forums.phpfreaks.com/topic/190506-regex-for-taking-in-array/#findComment-1004912 Share on other sites More sharing options...
satya61229 Posted February 1, 2010 Author Share Posted February 1, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/190506-regex-for-taking-in-array/#findComment-1004919 Share on other sites More sharing options...
RussellReal Posted February 1, 2010 Share Posted February 1, 2010 /<script[^>]*>(?:.*?<\/script>)?/i Quote Link to comment https://forums.phpfreaks.com/topic/190506-regex-for-taking-in-array/#findComment-1005120 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.