这段代码对我来说似乎非常简单和合乎逻辑,我一定很疲惫,因为它没有做我期望的事情。
//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”仍然会被打印出来?我做错了什么?
发布于 2011-08-12 07:10:11
试试count($regexMatches[0])
。
:)
发布于 2011-08-12 07:17:15
为什么不能直接使用preg_match_all返回值:
$matchCount = preg_match_all($regexTags, $string, $regexMatches);
echo $matchCount;
https://stackoverflow.com/questions/7036704
复制相似问题