1

I am trying to find HTML tags, via PHP ereg, that has a {xxx} right before the closing tag - where xxx can be [A-Za-z0-9_\-]*.

For example: <p>xxxx</p><p>yyyy{asdf}</p>

This is the best I could come up with: /<([\w]+)([^>]?)>([^{]*)\{([^}]+)\}<\/\1>/is

The problem is though, that it will match group 3 will have xxxx</p><p>yyyy, and I only want yyyy in this case.

Hope anybody can help, cheers, Egil.

Ps. Sorry for the useless title, could not think of anything better.

Toto
  • 89,455
  • 62
  • 89
  • 125
Egil Hansen
  • 15,028
  • 8
  • 37
  • 54
  • 2
    Never ever parse HTML with regex. Please check out DOMDocument from php.net – Mike Lewis Mar 17 '11 at 03:29
  • @Mike You forgot the obligatory link - http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Phil Mar 17 '11 at 03:30
  • The `ereg_*` suite has been deprecated for years and soon to be removed. Please use the [PCRE functions](http://www.php.net/manual/en/ref.pcre.php) – Phil Mar 17 '11 at 03:32
  • Phil, thanks, it is actually PCRE im using. – Egil Hansen Mar 17 '11 at 07:28

1 Answers1

3

Add a ? after each + and * in your regex. This will make the match less greedy.

Else try adding <> to the negative character classes [^{<>]* and [^}<>]* to exclude tags in between there.

mario
  • 144,265
  • 20
  • 237
  • 291