Page 1 of 1

Nested tags

Posted: Tue Jan 01, 2008 4:43 pm
by sachv
Hi,

I have a regex here

Code: Select all

"#<([a-z0-9-_]+?)>(.*?)</\\1>#is"
to catch tags (my own tags, not html) like <hello>some other text</hello>, which works fine.
But I want to handle nested tags like <hello><world>some text</world></hello>, and I now need to find a way to
let it match tags that do NOT contain any other tags inside them, like the first example.
I presume I'll have to exclude it from the (.*?) group, but I don't know how to implement it.
I tried

Code: Select all

"#<([a-z0-9-_]+?)>([^(<([a-z0-9-_]+?)>(.*?)</\\1>)]*?)</\\1>#is"
but it didn't work.

Any help is appreciated,

S.

Posted: Fri Jan 04, 2008 11:27 am
by arjan.top
something like this?

Code: Select all

#<([a-z0-9-_]+?)>(?!<([a-z0-9-_]+?)>)(.*?)</\\1>#is

Posted: Fri Jan 04, 2008 12:08 pm
by vigge89
http://php.net/manual/en/function.preg- ... llback.php
See Example#3 preg_replace_callback() using recursive structure to handle encapsulated BB code