TeNDoLLA Posted September 28, 2009 Share Posted September 28, 2009 Hi, I am trying to match an if-else patterns in templates and below is the closest I've gotten. It works if there is only one if-else in the template. But if there is more it will get everything starting from first IF to the last ENDELSE. I would like to capture the data for each if-else in the array. HTML example 1: <?php $html = '<div> {:if([:is_admin:]):} IS ADMIN {:endif:} {:else:} NOT AN ADMIN {:endelse:} </div>'; PHP example: <?php preg_match_all('/\{:if\((.*?)\):\}(.*?)\{:endif:\}.*\{:else:\}(.*?)\{:endelse:\}/s', $html, $matches, PREG_SET_ORDER); Result 1: Array ( [0] => {:if([:is_admin:]):} IS ADMIN {:endif:} {:else:} NOT AN ADMIN {:endelse:} [1] => [:is_admin:] [2] => IS ADMIN [3] => NOT AN ADMIN ) Now what I have here is: 0 = string to replace. 1 = variable to check for true/false 2 = text to put in place if true 3 = text to put in place if false Now if there is more than one if-else I get the whole string starting from first if to the last endelse. Say for example <?php $html = '<div> {:if([:is_admin:]):} IS ADMIN {:endif:} {:else:} NOT AN ADMIN {:endelse:} </div> <div> {:if([:another:]):} ANOTHER TRUE {:endif:} {:else:} ANOTHER FALSE {:endelse:} </div>'; It won't work anymore. And I am not able to get the regexp that way it would capture all occurances to the array separately. Anyone able to help me with this? Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted September 28, 2009 Share Posted September 28, 2009 I had a similar issue fairly recently. This linked thread outlines my solution. While the template syntax charcters are different, so you'd need to modify the regexp, and I was testing for is_admin == 'Y' rather than a simple boolean, the principle should still be similar enough. Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted September 28, 2009 Author Share Posted September 28, 2009 Thanks for reply Mark, I looked your example and because of the very different tags and structre you are using it was a little bit hard to read for me. I managed to solve the problem with adding one question mark between the if / else statements. In the middle of the regexp this part was before: {:endif:\}.*\{:else:\} and after (not greedy matching everything between the clauses) {:endif:\}.*?\{:else:\} Thanks for looking it though! Was almost the same thing as you were doing. Now the results are as I want them to be. Cheers. Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted September 28, 2009 Author Share Posted September 28, 2009 Actually now a new problem popped up. Now it matches multiple else-if's but if there is just a plain if without the else somewhere between it screws up. I should also make it so that it would match only else-if's and not plain if's. Tried adding there a negated character class for testing: [^:if] before the last endelse without success. I am not very expert with regular expressions. Anyone could point me out how can I add a pattern inside that regexp that must not be matched in certain parts? Certain parts being after the if started till the end facing endelse. After that there could be again new if-else starting. 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.