首页  >  问答  >  正文

关于抑扬符'^'的用法

<?php
header('Content-Type: text/html; charset=utf-8');
$pattern='/[^0-9A-Za-z_]/';
$string='!$@!#%$#^##';
if(preg_match($pattern, $string,$match)){
    echo '匹配到了,结果为:';
    var_dump($match);
}
else{
    echo '没有匹配到';
}
?>

输出:匹配到了,结果为:array(1) {  [0]=>  string(1) "!" }

我不明白$string中有很多不在[^0-9A-Za-z_]的范围之内,为什么只输出一个'!'呢


WillWill2509 天前2427

全部回复(2)我来回复

  • 寻觅 beyond

    寻觅 beyond2017-11-09 12:58:41

    preg_match()只匹配一次,如果匹配到符合条件的内容就立即返回,不再继续匹配了,即使后面还有符合条件的。

    回复
    0
  • 寻觅 beyond

    寻觅 beyond2017-11-09 12:57:35

    preg_match()只匹配一次,如果匹配到符合条件的内容就不再继续匹配,如果你要匹配所有符合条件的可以用preg_match_all($pattern,$string,$arr),$arr会保存匹配到的所有符合条件的内容

    回复
    0
  • 取消回复