i use this pattern '/(\{(\w+)\}(.*))?\{%(\w+)%\}((.*)\{\/(\w+)\})?/i' to extract tags from a template using preg_match function.
sample template:
<table id="middle" cellspacing="0px" cellpadding="0px">
{middle}
<tr>
{left}<td>{%left%}</td>{/left}
<td>{%middle%}{%content%}</td>
{right}<td>{%right%}</td>{/right}
</tr>
{/middle}
</table>
how to make sure that start and end of each tag truly match it’s name
in this example middle tag matches for both middle and content while it should just match middle tag
I think the best way to solve this problem would be do this in a few different steps.
First, you should use preg_replace_callback with
/(?>{([^}]+)})(.*)?{\/\1}/simas your regular expression. This will find the top level {tag}{/tag}. The $matches[2] will contain the content (without the tags) while $matches1 will contain the tag itself.You should create a function that’s called recursively so that in the callback, it’s called again on $matches[2] so find the children {tags} just in case there are more. This is how you’ll go through the tree.
Finally, you should make a third function that handles the {%tag%}. I would use preg_replace_callback again and make use of the switch statement to handle the tag name.
This should point you in the right direction.
EDIT: Here is a fully functional demo of what I described above:\
The resulting string will be:
<div><p>I like Francois Deschenes a <b>lot</b>.</p></div>.