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
{{if(pagetitle)?{{ print pagetitle || {{if(company_name)? print company_name|| print ''}} }}ifblocks. You cannot get them in 3 different matches because matches cannot overlap. You also cannot get them in 3 different captures of the same match, because for every capture group (i.e. every set of parentheses) in your pattern, there will always be exactly one capture. If the group is reused (through recursion) you will only the the inner- or outermost block. All you can do with regex is verify correct syntax. Parsing will have to be done separately (of a recursivepreg_replace_callback).