So in my PHP code I have a string like so;
The quick brown {{fox($a);}} jumped over the lazy {{dog($b);}}.
Now it might sound weird, but I want to go through the string, and collect all the BBCode style tags.
Then I want to eval() all the functions which are inside the {{}}‘s. So I’d eval fox($a); and dog($b);.
Both of these functions return a string. And I want to replace the respective tags with the respective results. So supposing fox() returns “vulpes vulpes” and dog() returns “canis lupus”, my original string would look like this;
The quick brown vulpes vulpes jumped over the lazy canis lupus.
However, I am famously terrible with regular expressions, and I have no idea how to go about this.
Any advice would be welcome!
(And yes, I am aware of the dangers of happy-go-lucky eval()ing. However, these strings come strictly from the developers and no user will ever be able to eval anything.)
If you want to do this with a regex, here’s a solution that seemed to work for me:
The regex is pretty simple:
Of course, the
/emodifier causes the replacement to beeval‘d, producing this:Output: