php - Replacing tags in braces, even nested tags, with regex -


example

preg_replace('/\{[a-za-z.,\(\)0-9]+\}/', 'replaced', 'lorem ipsum dolor sit {tag1({tag2()})}, consectetur adipiscing elit.'); 

the result:

lorem ipsum dolor sit {tag1(replaced)}, consectetur adipiscing elit.

question

as can see "tag2" has been replaced, want replace "tag1" know how can this?

(in cases might this:{tag1({tag2({tag3()})})}) , on.)

btw using preg_replace_callback, easier show preg_replace

here site can test code: http://www.spaweditor.com/scripts/regex/index.php

you need add curly braces character set. here's pattern used:

/\{[a-za-z.,\(\)\{\}0-9]+\}/ 

and here result:

"lorem ipsum dolor sit replaced, consectetur adipiscing elit." 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -