Page 1 of 1

matching nested <ul> lists

Posted: Sun Aug 17, 2008 3:59 pm
by rocco2004
Hello,

I need to properly match a <ul></ul> block in list menu. If I try to determine the end of the block by the first closing </ul> I come to I'll get a false positive if it hits the closing </ul> of an inner nested <ul> block.

Somehow, the comparison needs to keep count of how deep it is inside the nesting, and make sure the number of closing </ul>s equals the number of opening <ul>s before it considers the pattern matched.

Thanks in advance for your help!
Rocco

Re: matching nested <ul> lists

Posted: Mon Aug 18, 2008 1:01 am
by dancing dragon
rocco2004 wrote: Somehow, the comparison needs to keep count of how deep it is inside the nesting, and make sure the number of closing </ul>s equals the number of opening <ul>s before it considers the pattern matched.
I think your answer is in your question. Have a variable keep count of how deep the parsing is inside the nesting. When you hit an opening <ul>, depth++. When you hit a closing </ul>, depth--, relative from your starting point of depth = 0.

Is that what you're looking for?

Re: matching nested <ul> lists

Posted: Mon Aug 18, 2008 9:06 am
by rocco2004
Hello again,

Yes, counting nested ul may be an option but I'm not sure what function to use. I need to somehow loop through ul elements within the string and count their deepens. How should I do that?
Thanks
Rocco

Re: matching nested <ul> lists

Posted: Tue Aug 19, 2008 7:32 pm
by dancing dragon
How about something like function preg_split where you split on the regular expression of <ul> OR </ul>? That would be something like "/(<ul>)|(<\/ul>)/" . (I haven't tested that. There could be bugs in there.)

And you would probably need to use the flag PREG_SPLIT_DELIM_CAPTURE to know which delimiter each split was made on.

That's one idea. I don't know if there are better options.