Author Topic: Regex wizards - matching same number of opening and closing tags  (Read 505 times)

0 Members and 1 Guest are viewing this topic.

Offline friziTopic starter

  • Irregular
  • Posts: 23
  • Gender: Male
  • Where there's a will, there is a way.
    • View Profile
    • Personal page
Hi

It would  be great if you could help me with my problem. I would like to use a regex expression (or a php script) that would select one complex tag. The complexity at this is that the tag could contain the same tag inside (at menu content for example - ul li ul).

Therefor I need a special code that would search so many closing tags as many opening tags for matching the following first UL:

<ul> <li> first </li>
   <li> <ul>
   <li> second </li>
   <li> <ul> third </ul> </li> </ul> </li>
   <li> <ul> <li> fourth </li> </ul> </li>
   <li> end of selection </li>
</ul>
<ul> this should be not selected </ul>

I need to search so many opening tags as closing tags from the first tag (ul) until next different tag.
It was told me that I should use Dom document. This is not possible because I'm working with transitional style html style and I need just the first level (main) tags.

Thank you in forward.

Offline salathe

  • Lazy
  • Administrator
  • Addict
  • *
  • Posts: 1,540
  • Gender: Male
  • Temperament: Snuggly
    • View Profile
    • My Blog
Re: Regex wizards - matching same number of opening and closing tags
« Reply #1 on: March 12, 2010, 07:44:38 AM »
You should be using the DOM. If you're having trouble doing that, please ask questions.

Any regex solution will be far more complicated and difficult to maintain and understand in the future (or even, now).
PHP Documentation — Read it, or the badger will come for you.
http://php.net/manual/