x1nick Posted August 25, 2009 Share Posted August 25, 2009 Hi I have different variables {include file.php} {include admin file.php} {include temp file.php} What i need from the regex is for it to return the 'admin' (or what ever it might be) from the variable separately into the array This is my current bit of code im experimenting with $data = "{include file.php} {include admin file.php} {include temp file.php}"; preg_match_all('/{include (?:admin([^}]*))?([^}]*)}/Uis', $data, $matches); echo '<pre>'; print_r($matches); I have only put admin in there as im unsure on how to do this correctly Currently the array looks like Array ( [0] => Array ( [0] => {include file.php} [1] => {include admin file.php} [2] => {include temp file.php} ) [1] => Array ( [0] => [1] => [2] => ) [2] => Array ( [0] => file.php [1] => admin file.php [2] => temp file.php ) ) What I want it to look like: Array ( [0] => Array ( [0] => {include file.php} [1] => {include admin file.php} [2] => {include temp file.php} ) [1] => Array ( [0] => [1] => admin [2] => temp ) [2] => Array ( [0] => file.php [1] => file.php [2] => file.php ) ) Hope that makes sense Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 26, 2009 Share Posted August 26, 2009 One quick possible solution could be: $data = "{include file.php} {include admin file.php} {include temp file.php}"; preg_match_all('#{include ([a-z]+ )?([a-z]+\.[^}]+)}#i', $data, $matches); echo '<pre>'.print_r($matches, true); If there could be stuff like underscores, numbers or what have you, simply add them into the character classes. This all makes rigid assumptions about spacing / formating (like a single space delimiting each set of data). Quote Link to comment Share on other sites More sharing options...
.josh Posted August 26, 2009 Share Posted August 26, 2009 bit more flexible.. ~\{include\s(?[^}]+)\s)?([^}]+)~i Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 26, 2009 Share Posted August 26, 2009 bit more flexible.. ~{includes(?[^}]+)s)?([^}]+)~i True that.. Probably need to include the final closing curly brace in the pattern, otherwise, all the results from index 0 have this character missing (and no need to escape {). Quote Link to comment Share on other sites More sharing options...
.josh Posted August 26, 2009 Share Posted August 26, 2009 yeah I noticed that after I posted. figured it wasn't worth going back to edit, that he should probably be able to figure that out on his own. I hate going back to edit. aside potentially causing confusion, I feel it somehow cheapens my post on some level. This, coming from a guy who shamelessly spams like a 13 year old script kiddie with mommy issues. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 26, 2009 Share Posted August 26, 2009 hehe.. I understand. Quote Link to comment Share on other sites More sharing options...
x1nick Posted August 27, 2009 Author Share Posted August 27, 2009 Hi thanks for the input, works perfectly Got another problem with regex, although this one more likely is the way im doing things. I have the following {permission abc} {/permission} What I want to be able to do is have another {permission nested inside (and possibly another and so on), like this {permission abc} {permission def} {/permission} {/permission} If this at all possible with regex? Quote Link to comment Share on other sites More sharing options...
.josh Posted August 27, 2009 Share Posted August 27, 2009 regex is difficult at best when dealing with nested tags like that. If you know exactly how many times something can be nested, you can with much effort use regex to parse it. But if it can be nested any number of levels deep, don't bother trying to do it with regex. I suggest you look into redoing your tag syntax and using an xml or dom parser class. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted August 27, 2009 Share Posted August 27, 2009 If you are replacing the tags with e.g. (X)HTML, you can deal with the opening and closing tags separately. Have a read here: http://www.phpfreaks.com/forums/index.php/topic,205904.msg934306.html#msg934306 Quote Link to comment Share on other sites More sharing options...
x1nick Posted August 28, 2009 Author Share Posted August 28, 2009 Hi They are being replaced with php if ($cms->permission('###permission value###') { and closing {/permission} } I might look at doing something like that, and counting the number of opens to the number of closes to try calculate if it contains all the rightclosing points. Unless there could be a better way Suppose I could do a search for the opening tag, then once that's found look for a closing tag. If both are found then replace them. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted August 28, 2009 Share Posted August 28, 2009 When it's building PHP, I think the code should spit out an error if the tags don't match up, as automatically adding or removing closing tags to make them match would create unexpected results in most cases. Here's a simple example: <?php //$source contains the source string $temp = preg_replace('~{permission\s+([^}]+)}~i', 'if ($cms->permission(\'$1\')) {', $source, -1, $opening_count); $temp = str_ireplace('{/permission}', '}', $temp, $closing_count); if ($opening_count === $closing_count) { //if a matching number of tags were replaced, overwrite $source with the modified string $source = $temp; } else { //error trigger_error('Opening and closing tags don\'t match up', E_USER_ERROR); } ?> Quote Link to comment Share on other sites More sharing options...
x1nick Posted August 28, 2009 Author Share Posted August 28, 2009 Thanks, will play around with it and see what happens 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.