0

The following code is working as intended:

<?php
preg_match_all('|<[^>]+>.*</[^>]+>|U',
    "<b>example: </b><div align=left>this is a test</div>",
    $out);
echo $out[0][0] . ", " . $out[0][1] . "\n";
?>

It matches the first b tag and then moves on to match the div tag as it should. However, when I use a backreference only the b tag is matched.

<?php
preg_match_all('|<([^>]+)>.*</\1>|U',
    "<b>example: </b><div align=left>this is a test</div>",
    $out);
echo $out[0][0] . ", " . $out[0][1] . "\n";
?>

What is incorrect here?

This question has been marked as duplicate. I read the post but was not convinced it actually was a duplicate question. However, the posted comment was helpful and made me understand my error. Thanks.

Danny
  • 11
  • 3
  • You need to match the tag name and not the attributes into your capturing group. that or use a propper HTML parser like DOMDocument. `<([^>]+)>` is capturing `div align=left` so you're asking it to match `` – Scuzzy Jun 30 '18 at 12:12
  • Since you're usingn `U` try changing `<([^>]+)>` to `<([^>]+)(?:\s[^>]+)?>` – Scuzzy Jun 30 '18 at 12:18

0 Answers0