HabibS HabibS - 6 months ago 11
PHP Question

how to get nested values inside braces

How to use php to get inside nested braces?

example:

{{ text1 {{text2 text3 {{text4}} text5}} }}


should output

1- text1 {{text2 text3 {{text4}} text5}}
2- text2 text3 {{text4}} text5
3- text4

Answer

I've found the answer i was looking for and put this here so everyone could use it. Its very simple indeed, in one line only:

  $text1=preg_replace("/\{\{(([^{}]*|(?R))*)\}\}/",'',$text1);

It will search and replace all {{text}} with whatever you want. You can also use the preg_match_all to get all of them in an array.

Comments