tags:

views:

106

answers:

2

Hello!

I'd like to mimick (reproduce) Expression Engine's fantastic template parsing method. (Pls don't ask me why not using it :))

While i'm able to find and parse simple tags like

{example_param = "param_value"}

i cannot parse tags where closing tag added:

{cyclic_param}
...
{/cyclic_param}

This is the pattern i'm using:

'/[\{^\/](.*)\}/iU'

but it's returns {/cyclic_param} too.

I know there are zillions of regexp tutors out there but this is the thing i cannot understand ever :( (And i cannot figure out from EE's source)

How can i find opening and closing tags (with their inner blocks too) with PHP's regexp?

Thanks for your help!

+1  A: 
 preg_match('~{(\w+)}(.+?){/\1}~s', $r, $m);

content will be in $m[2].

this won't handle nesting though.

/edit: complete example

 $text = "
 foo {single1=abc}
 bar {double1} one {/double1}
 foo {single2=def}
 bar {double2} two {/double2}


 ";

 $tag = '~
  {(\w+)}(.+?){/\1}
  |
  {(\w+)=(.+?)}
 ~six';

 preg_match_all($tag, $text, $m, PREG_SET_ORDER);
 foreach($m as $p) {
  if(isset($p[3]))
   echo "single $p[3] with param $p[4]\n";
  else
   echo "double $p[1] with content $p[2]\n";
 }
stereofrog
nice one but this one isn't find single tags. maybe i need to run through my template twice? first find double tags then again for single ones?
fabrik
extended with an OR statement, it's getting close but it's not okay yet: ~\{(.*)\}|{(.*)}(.+?){/\1}~s
fabrik
added more details to the post
stereofrog
it's fantastic! many thanks for you!
fabrik
A: 

I think this is proper for You:

'/\{[^\/]*\}/iU'
Kamilos
this one returns the first single tag only :(
fabrik