christian christian - 2 months ago 14
PHP Question

Looking for a PHP template Parser with nesting

I am looking for a PHP parser that can do this.

{tag} Replace the tag with text coming from a function

{tag(params)} It must support params

{tag({tag(params)},{tag(params)})} It must support nesting



} It must support Tests

{$tag=value} It must support varriables

Does anyone know of a parser that can do this?

Or maybe you know how I can create one. I have tried to do this with preg, but it seems impossible to create nesting.

Smarty seems to be a bit to big, and I don't know if you can disable all the extra functionality it has. I only need the functionality that I have listed above.

In smarty you're able to write PHP code and I don't like that. {php} {/php}
So if I'am going to use that I need to be able to turn it of.

I'm going to use it with CodeIgniter.


To incorporate nesting in your regular expressions you have to use recursive patterns (?R). This matches whole pattern.

To find outermost {}-s in text (possibly with some other {} inside of them) you should use expression like this:


You can read about (?R) here:

There is a construct {((?>[^{}]+)|(?R))*} suggested there. Maybe more efficient than the one I built.