matching nested <ul> lists

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rocco2004
Forum Newbie
Posts: 5
Joined: Sun Aug 17, 2008 3:55 pm

matching nested <ul> lists

Post 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
dancing dragon
Forum Newbie
Posts: 8
Joined: Sun Aug 17, 2008 4:57 am
Location: bed

Re: matching nested <ul> lists

Post 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?
Last edited by dancing dragon on Mon Aug 18, 2008 9:09 am, edited 1 time in total.
rocco2004
Forum Newbie
Posts: 5
Joined: Sun Aug 17, 2008 3:55 pm

Re: matching nested <ul> lists

Post 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
dancing dragon
Forum Newbie
Posts: 8
Joined: Sun Aug 17, 2008 4:57 am
Location: bed

Re: matching nested <ul> lists

Post 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.
Post Reply