Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_match_都没有做我期待的事情?_Php_Regex_Count_Preg Match All - Fatal编程技术网

Php preg_match_都没有做我期待的事情?

Php preg_match_都没有做我期待的事情?,php,regex,count,preg-match-all,Php,Regex,Count,Preg Match All,这段代码对我来说似乎非常简单和合乎逻辑,我一定很累了,因为它没有达到我期望的效果 //look for any letter, number or underscore $regexTags = "/[a-z0-9_]/i"; //needlessly large string.. $string = "111 222 333 444 555 666 777"; preg_match_all($regexTags, $string, $regexMatches); print_r($r

这段代码对我来说似乎非常简单和合乎逻辑,我一定很累了,因为它没有达到我期望的效果

//look for any letter, number or underscore    
$regexTags = "/[a-z0-9_]/i";

//needlessly large string..
$string = "111 222 333 444 555 666 777";
preg_match_all($regexTags, $string, $regexMatches);
print_r($regexMatches);

//$regexMatches spits out  [0] => Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 2 [4] => 2 [5] => 2 [6] => 3 [7] => 3 [8] => 3 [9] => 4 [10] => 4 [11] => 4 [12] => 5 [13] => 5 [14] => 5 [15] => 6 [16] => 6 [17] => 6 [18] => 7 [19] => 7 [20] => 7))
//as expected.

//Count how many variables are in the array 
$matchCount = count($regexMatches);

if ($matchCount < 5){
    echo "less than 5";
}
为什么$regexMatches数组中显然有20个值,但仍打印少于5个值?我做错了什么?

尝试计数$regexMatches[0]


为什么不能使用preg\u match\u all返回值:

$matchCount = preg_match_all($regexTags, $string, $regexMatches);
echo $matchCount;

高高在上!!我花了整整两个小时试图弄明白这一点,而不必花一点时间去打扰stack overflow的好人。谢谢,这就是答案。嘿,很高兴能帮上忙。我知道我们都有过这样的经历:pOh,这只是一个用最简单的形式解释我的问题的例子=我的项目代码看起来与此不同=谢谢你的建议,不过^^^哦,仔细想想,我也没有尝试过,它很好地整理了我的代码。干杯=当你不需要额外依赖性能的时候,这很好:D