首页 > 解决方案 > PHP regex backreference not matching properly

问题描述

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.

标签: phpregexbackreference

解决方案


推荐阅读