Hi I’m having some problems with php recurring regexp. I have strings like
{{if(x=y)? {{ true do something || false do something else }}
in my html files. It will be some kind of basic template engine. If I use
$matches=array();
$content = "{{if(x=y)? true do something || false do something else }}";
preg_match_all('/\{\{if\((.*?)\)\?(.*?)\|\|(.*?)\}\}/is',$content,$matches);
returns the results as I expected.
Array(
[0] => Array
(
[0] => {{if(x=y)?
true do something
||
false do something else
}}
)
[1] => Array
(
[0] => x=y
)
[2] => Array
(
[0] => true do something
)
[3] => Array
(
[0] => false do something else
)
)
But if the pattern is nested with another one like;
{{if(x=y)?
{{if(y=z)?
true do something
||
false do something else
}}
||
{{if(x=a)?
true do something
||
false do something else
}}
}}
it takes the first “}}” chars as end of pattern and fails
Array
(
[0] => Array
(
[0] => {{if(x=y)?
{{if(y=z)?
true do something
||
false do something else
}}
)
[1] => Array
(
[0] => x=y
)
[2] => Array
(
[0] => {{if(y=z)?
true do something
)
[3] => Array
(
[0] =>
false do something else
)
)
I would like to make a recurring regexp so in each part of true or false it should check if the matched content has same pattern again. The logical part of If is already done. I just need a regexp will match the parts so I can loop thru the results.With my Regexp knowledge this is all I could do so far.EDIT
to be more descriptive I need a regexp can parse something like this.
{{if()?{{if()?{{if()?...||...}}||{{if()?...||...}}}}||{{if()?{{if()?...||...}}||{{if()?...||...}}}}}}
but the regexp I used can only catches from first {{if to first }} it finds which returns
{{if(){{if(){{if()...||...}}
which is correct for the regexp. But how can I rule the regexp as “get whole text, find the block between {{if()? and }} till the end and ignore any others if it is not at the end” or “get the most outer {{if()?||}} block.
Thanks
I solved the problem with using link. Author of the script made it simple enough with a different style. In my sample I was trying to match the code blocks with regexp and replace them with the data, but he actually replaced the code blocks into php codes and recreates the files in a different folder with same structure and each time page requested, first checks the file existence and updated time and runs the script. pretty smart. Maybe not a perfect solution and rational but solves my problem. thanks anyway