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
)
)