The Little Guy Posted June 5, 2012 Share Posted June 5, 2012 I made a minifier (kinda), basically it removes newlines/tabs/2+ spaces in the html doc. What I would like to do is remove js comments in the html doc. BUT! I display php code on the page so I can not remove those comments. so... I would like to remove comments between the <script> and <style> tags, and ignore the ones between the <code> tags. Any suggestions on how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/263719-remove-javascript-comments/ Share on other sites More sharing options...
scootstah Posted June 5, 2012 Share Posted June 5, 2012 Why not copy/paste the code between those tags into the minifier, and then copy/paste it back afterwards? Quote Link to comment https://forums.phpfreaks.com/topic/263719-remove-javascript-comments/#findComment-1351447 Share on other sites More sharing options...
The Little Guy Posted June 5, 2012 Author Share Posted June 5, 2012 That is kinda what I just started to do, then write a lexer on it. If there is a better/faster or any other suggestions way please let me know! Quote Link to comment https://forums.phpfreaks.com/topic/263719-remove-javascript-comments/#findComment-1351450 Share on other sites More sharing options...
The Little Guy Posted June 5, 2012 Author Share Posted June 5, 2012 Is it me, or is this being greedy? preg_match_all("/<script.+>.+<\/script>/isU", $template, $matches); foreach($matches[0] as $match){ if(!empty($match)){ echo "\n\n\n\n\n\n\n\n\n$match\n\n\n\n\n\n\n\n\n"; } } Output: <script type="text/javascript" src="//jssnips.com/jquery/latest.js"></script><script type="text/javascript" src="//static1.phpsnips.com/js/anchorReader.js"></script> I have about 5 - 10 places where script tags are located on a page, why is it pulling 2 script tags out as one match? They should each be their own match, and those two tags should have even matched. Quote Link to comment https://forums.phpfreaks.com/topic/263719-remove-javascript-comments/#findComment-1351463 Share on other sites More sharing options...
kicken Posted June 5, 2012 Share Posted June 5, 2012 Your .+ between the script tags in the regex requires that there be at least one character between the start and end tag for a match. Since you have no characters between your start and end tags the match doesn't work on the individual pairs. It matches both pairs as a whole because it counts the end/start tags as the required content in the middle. Use .* for a zero or more match. Quote Link to comment https://forums.phpfreaks.com/topic/263719-remove-javascript-comments/#findComment-1351466 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.