Wuhtzu Posted February 20, 2008 Share Posted February 20, 2008 Hey I'm trying to match and grab css comments from .css files, but I'm having troubles getting it 100% right. The comments look like: /* This is a css comment */ /* This is a multiline css comment ============================*/ /* Troublesome * / css comment containing characters from the closing tag */ This will match all the comments but the ones containing a * anywhere other than in the opening and closing tag. I could do the same with with the / (slash) but it would do the same thing. This is a problem since css comments can contain both * (stars) and / (slashes). Notice @ is the delimiter (ran out of better ideas) '@(/\*[^*]*\*/))@s' Another thing which obviously does not work is: '@(/\*.*\*/))@s' Since it will match anything between the first opening tag and the last closing tag in the document, including other comments. So this will basically return the whole style sheet. Can you guys think of a way to match those comments? It seems doable: http://www.lonniebest.com/FormatCSS/ Best regards Wuhtzu Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 20, 2008 Share Posted February 20, 2008 This worked fine for me: <?php $css = "/* This is a css comment */ /* This is a multiline css comment ============================*/ /* Troublesome * / css comment containing characters from the closing tag */"; if(!preg_match_all('/\/\*(.+?)\*\//s',$css,$matches)) die("Could not match"); print_r($matches[1]); ?> output: Array ( [0] => This is a css comment [1] => This is a multiline css comment ============================ [2] => Troublesome * / css comment containing characters from the closing tag ) Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted February 20, 2008 Author Share Posted February 20, 2008 wow... why didn't I think of that? It works like a charm What are you doing with your (.+?) ?? It's some kind of laziness / greediness, right? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 20, 2008 Share Posted February 20, 2008 Yeah, the ? limits the greediness of .+ Quote Link to comment 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.