donglu5047 2017-05-29 22:23
浏览 41
已采纳

php preg_match_all()没有按预期工作

In the example string below I have 5 times #A# and 11 times #B# but preg_match_all returns only one match for #A# and 0 matches for #B#. Any idea how to solve this ?

$string = '<div>#Α#</div><div i="">#Α#</div><div i=""><a href="#Β#" class="" s="" p="65" t="4"></a><a href="#Β#" class="" s="" p="65" t="4"></a><a href="#Β#" class="" s="" p="68" t="4"></a><a href="#Β#" class="" s=""></a><a href="#Β#" class="" s="" p="75" t="2"></a><a href="#Β#" class="" s=""></a><a href="#Β#" class="" s="" p="65" t="3"></a><a href="#Β#" class="" s="" p="60" t="4"></a><a href="#Β#" class="" s=""></a><a href="#Β#" class="" s="" p="60" t="4"></a><a href="#Β#" class="" s="sv_plus" p="75" t="3"></a><noscript><center><h1 style="color:red;font-size:50px">#A#</h1></center><div>#Α#</div></noscript><div>#Α#</div>';

preg_match_all("/\#A\#/",$string,$matches);
print_r($matches); //return only one match instead 5

preg_match_all("/\#B\#/",$string,$matches);
print_r($matches); //return zero matches instead 11
  • 写回答

1条回答 默认 最新

  • drxrgundk062317205 2017-05-29 22:35
    关注

    It's because your string only contains #A# once and doesn't contain #B#.

    It does contain #Α# four times and #Β# 11 times though.

    The difference is that the second versions contain a Greek Capital Letter Alpha and a Greek Capital Letter Beta, not ascii A and B.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?