2011-08-12 13 views
0

このコードは、私にはとても簡単かつ論理的なようで、私がそれをすることを期待何をしていないので、私は疲れている必要がありますが...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($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"; 
} 

なぜ「5未満」はまだ20はかなり明確にありますにもかかわらず、印刷されています$ regexMatches配列の値は?私は間違って何をしていますか?

答えて

4

Try count($regexMatches[0])

:)

+1

GAUGHHHGHGHGHGHGHHGHGHGHGHGHGHGHGHを! *髪を引っ張る*良い時間を過ごした人がスタックオーバーフローすることを気にせずに、それを理解しようとすると、2時間かけて過ごした。ありがとう、それは答えでした。 – Partack

+1

こんにちは、喜んで助けてください。 p – Kokos

3

なぜあなただ​​けpreg_match_all戻り値を使用カント:

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

ああ、これは私の問題を簡単な形式で説明するための単なる例であった=)私のプロジェクトコードはこれとは違って見える=)あなたの提案に感謝します^^ – Partack

+0

ああ、私はこれも試していないし、うまくコードを整理しています。乾杯。 =) – Partack

+1

パフォーマンスに余計にカウントする必要がない場合は、D – Vytautas

関連する問題