1

I need to make a regex which matches content between tags like this:

<tag>
    <b> Match Me </b>
</other-closing-tag>

It should match only the content between the same tag. So the result should be something like this:

1 match:
<b> Match Me </b>
Match me

And I need to do it in PHP, but I don't think that this is that important...

j0k
  • 22,600
  • 28
  • 79
  • 90
Itay Grudev
  • 7,055
  • 4
  • 54
  • 86
  • If you're trying to parse HTML, this might be helpful: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Toomai Dec 20 '11 at 15:24
  • @Toomai Not exactly html but an html-like structure. Anyway thanks ... – Itay Grudev Dec 20 '11 at 16:10

1 Answers1

2

Use regular expression back references, which you can read more about under the following link.

Though parsing html with regular-expressions is never a good idea, but I'm going to pretend that you are going to use this information for a completely different problem. ;-)


Example snippet

In the below we are saying that the contents of our end-tag should be the same as what is matched by our first group ([^>]+), by using \1 inside our closing tag.

$data =<<<EOT
  <awesome-tag> match-me </awesome-tag>
  <error-tag>   match-me </err0r-tag>
  <super-tag>   match-me </super-tag>
  <error-tag>   match-me </err0r-tag>
  <awesome-tag> match-me </awesome-tag>
EOT;

preg_match_all ('/<([^>]+)>.*?match-me.*?<\/\1>/s', $data, $matches);

print_r ($matches);

output

Array
(
  [0] => Array
  (
    [0] => <awesome-tag> match-me </awesome-tag>
    [1] => <super-tag>   match-me </super-tag>
    [2] => <awesome-tag> match-me </awesome-tag>
  )

  [1] => Array
  (
    [0] => awesome-tag
    [1] => super-tag
    [2] => awesome-tag
  )
)
Community
  • 1
  • 1
Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196