Hey everyone, I’m working on a PHP application that needs to parse a .tpl file with HTML in it and I’m making it so that the HTML can have variables and basic if statements in it. An if statement look something like this: `
<!--if({VERSION} == 2)--> Hello World <!--endif -->
To parse that, I’ve tried using preg_replace with no luck. The pattern that I tried was
/<!--if\(([^\]*)\)-->([^<]*)<!--endif-->/e
which gets replaced with
if($1) { echo '$2'; }
Any ideas as to why this won’t work and what I can do to get it up and running?
I think you meant to do this:
Your regex has only one character class in it:
Here’s what’s happening:
So, after removing the duplicates and sorting the characters into their ASCII order, your character class is equivalent to this:
And the parentheses outside the character class are still balanced, so the regex compiles, but it doesn’t even come close to matching what you wanted it to.