canadabeeau Posted February 24, 2010 Share Posted February 24, 2010 Hi Guys, I need a PHP function which will place into and array all text that does not sit between { or }, eg #header_crm a { display:block; height:260px; margin:0px; padding:0px; } it would add to the array "#header_crm a" only and not the other content, any ideas. I have tried explode("{", $file_contents); but it still displays the other stuff even if I use "{,}" As usual thanks for all the help in advance Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted February 24, 2010 Author Share Posted February 24, 2010 I am using this code, but the problem is there is multiple cases of text that lies between { and } and this code only does one instance function ExtractString($str, $start, $end) { $str_low = strtolower($str); $pos_start = strpos($str_low, $start); $pos_end = strpos($str_low, $end, ($pos_start + strlen($start))); if ( ($pos_start !== false) && ($pos_end !== false) ) { $pos1 = $pos_start + strlen($start); $pos2 = $pos_end - $pos1; return substr($str, $pos1, $pos2); } } $array = ExtractString($file_contents, '{', '}'); echo $array; Quote Link to comment Share on other sites More sharing options...
salathe Posted February 24, 2010 Share Posted February 24, 2010 You could use a string tokenizer to move through the string piece by piece. Take a look at the strtok function, let us know if it makes (no!) sense to you. Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted February 24, 2010 Author Share Posted February 24, 2010 My aim is to have a PHP script read and extract data from the CSS and place into a DB, so does anyone have an idea or solution, the strtok breaks it up into little pieces so I dont think that really helps, unless you can suggest a reliable way to stich it back together Thanks again Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted February 24, 2010 Author Share Posted February 24, 2010 bump Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted February 25, 2010 Author Share Posted February 25, 2010 this is now getting rather urgent, so any help would be greatly appreciated Quote Link to comment Share on other sites More sharing options...
salathe Posted February 25, 2010 Share Posted February 25, 2010 What have you tried so far, and why isn't that good enough (or working)? Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted February 25, 2010 Author Share Posted February 25, 2010 Well I tried on thing and it only took the first text before the {, so that is no good I need something that will collect text before { (but after the previous }) and the text between {} Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted February 27, 2010 Author Share Posted February 27, 2010 bump Quote Link to comment Share on other sites More sharing options...
salathe Posted February 27, 2010 Share Posted February 27, 2010 Please in future, if you want anyone to help, be has helpful to us as possible. Even after being specifically asked for more details you still didn't say what you had tried (so we could fix it) nor did you pick it up and run when given a very strong hint towards an easy solution. You can split up the CSS selectors and declarations using strtok (looking for curly braces) like: $tok = strtok($subject, "{}"); while ($tok !== FALSE) { $toks[] = $tok; $tok = strtok("{}"); } That will simply give a long list of selector 1, declarations 1 … selector n, declarations n. If you want the parts in a different array structure (or, however you want them!) then you can change the tokenising process accordingly or work on that array, whichever suits your needs. Since this is in the regex forum here's a regex which also splits up the different parts: preg_match_all('/([^{]+){([^}]+)}/', $subject, $matches, PREG_SET_ORDER); Go wild. 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.