mentalist Posted August 17, 2014 Share Posted August 17, 2014 In this example i'm scanning a css file for colour hash codes... $match='/(#[a-f0-9]{3,6})/is'; // matches colour hashes from 3 to 6 in length preg_match_all($match, $s, $a, PREG_PATTERN_ORDER);That works ok for now, what i'm after is how once matched as above how to get some text from before the mmatch returned as a seperate group. Example css: body { background: #fff; } a { color: #000; text-decoration: none; } At the moment i'm just returned "#fff" and "#000", what i'm after is ("#fff","body") and ("#000","a"). So basically after match come back to before "{" and either to start of file or next "}". Any help please? Quote Link to comment https://forums.phpfreaks.com/topic/290499-after-match-lookbehind-and-get-some-of-that-too/ Share on other sites More sharing options...
requinix Posted August 17, 2014 Share Posted August 17, 2014 What about .class1 .class2 { /* used to be #000 */ color: #0f0f0f; }and .class1, .class2 { border: 1px solid #999; }What do you want to get from that? Quote Link to comment https://forums.phpfreaks.com/topic/290499-after-match-lookbehind-and-get-some-of-that-too/#findComment-1488077 Share on other sites More sharing options...
mentalist Posted August 23, 2014 Author Share Posted August 23, 2014 Yup the first one would be an annoying anomaly, whereas in the second ".class1, .class2 " So for now ignore the comments issue... Any example on how to grab previous data would be great thanks Quote Link to comment https://forums.phpfreaks.com/topic/290499-after-match-lookbehind-and-get-some-of-that-too/#findComment-1488656 Share on other sites More sharing options...
Jacques1 Posted August 23, 2014 Share Posted August 23, 2014 I understand that you like regexes and want to use them for this task, but they're simply not the right tool. Not even by the lowest standards. Comments are just one “anomaly”. You may also run into ID selectors or URL fragments or whatever. The whole thing is basically a shot in the dark. And since you've made the task even more complex, I fearly you'll quickly end up with a complete mess of buggy, unreadable regexes which nobody can possibly maintain. For what? CSS is an actual language, it has a very specific structure. So what you should do is grab a CSS parser (there are plenty) and make it build a syntax tree. This gives you all the data you need in the right context. Quote Link to comment https://forums.phpfreaks.com/topic/290499-after-match-lookbehind-and-get-some-of-that-too/#findComment-1488659 Share on other sites More sharing options...
mentalist Posted August 23, 2014 Author Share Posted August 23, 2014 Good point but any which way how does this scenario work with regex please? Basicaaly capture anything from } to { (or start of file) prior to the match and working backwards. Quote Link to comment https://forums.phpfreaks.com/topic/290499-after-match-lookbehind-and-get-some-of-that-too/#findComment-1488661 Share on other sites More sharing options...
mentalist Posted August 23, 2014 Author Share Posted August 23, 2014 @requinix: The comments could be preparsed / stripped out. @Jacques1: I'll have a crack at my own simplified css parser later More than what was needed for this hack but it'll serve useful... Quote Link to comment https://forums.phpfreaks.com/topic/290499-after-match-lookbehind-and-get-some-of-that-too/#findComment-1488720 Share on other sites More sharing options...
mentalist Posted August 24, 2014 Author Share Posted August 24, 2014 It could be more adaptive or even maybe written in a one liner, but here's my quick hack... <?php // Simple CSS Parser // www.rawstar7.co.uk/site $fn="tmp/style.css"; $f = fopen($fn, "rb"); $contents = fread($f, filesize($fn)); fclose($f); $a=css_parse($contents); $out=css_print($a); function css_parse($s){ // STRIP OUT COMMENTS (Can be done elsewhere or missed out completely!) $s = preg_replace('!/\*.*?\*/!s', '', $s); $s = preg_replace('/\n\s*\n/', "\n", $s); // SPLIT BY "}" $a=explode("}",$s); $acss=array(); foreach($a as $e){ // SPLIT BY "{" $aa=explode("{",$e); // SPLIT NAMES LIST $anames=preg_split("/[\s,]+/", trim($aa[0])); // SPLIT TAGS LIST $atags=explode(";",$aa[1]); // SPLIT INDIVIDUAL TAGS INTWO K => V PAIRS // BUNDLE IT UP $acss[]=array($anames,$atags); } return $acss; } function css_print($a){ $sret= "<table>"; foreach($a as $e){ $sret.= "<tr><td>"; foreach($e[0] as $ee){ $sret.= $ee."<br />"; } $sret.= "</td><td>"; foreach($e[1] as $ee){ $sret.= $ee."<br />"; } $sret.= "</td></tr>\n"; } $sret.= "</table>"; return $sret; } ?> <html><head> <style> table,th,td { border: 1px solid #000; vertical-align: text-top; } </style> </head><body> <?php echo $out; ?> </body></html> Quote Link to comment https://forums.phpfreaks.com/topic/290499-after-match-lookbehind-and-get-some-of-that-too/#findComment-1488805 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.