Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex - get elements to render if statement

Tags:

I'm designing a script and trying to get to the if construct without eval in php.

Still incomplete but blasting through, it's to do a templating engine, the "if" part of the engine. no Assignment operators allowed, but I need to test values without allowing php code injections, precisely not using eval It'll need to do individual operations between variables preventing injection attacks.

Regex must capture

[if:(a+b-c/d*e)|(x-y)&!(z%3=0)]
    output
[elseif:('b'+'atman'='batman')]
    output2
[elseif:('b'+'atman'='batman')]
    output3
[elseif:('b'+'atman'='batman')]
    output4
[else]
    output5
[endif]

[if:(a+b-c/d*e)|(x-y)&!(z%3=0)]
    output6
[else]
    output7
[endif]

The following works to get the if, elseif, else and endif blocks along with the condition statements:

$regex = '^\h*\[if:(.*)\]\R(?<if>(?:(?!\[elseif)[\s\S])+)\R^\h*\[elseif:(.*)\]\R(?<elseif>(?:(?!\[else)[\s\S])+)\R^\h*\[else.*\]\R(?<else>(?:(?!\[endif)[\s\S])+)\R^\[endif\]~xm';

Please help in having optional elseif and else.

then with the condition statement, I can get the operations with:

$regex = '~([^\^=<>+\-%/!&|()*]+)([\^+\-%/!|&*])([^\^=<>+\-%/!&|()*]*)~';

however, it'll only pair them, missing each 3rd operator...

Thanks for your help.