2010-12-15 71 views
1

我正在試驗explode()失敗的東西,所以我想嘗試其他的東西。如果我有一個字符串,我怎麼能算在它的字符被假設的數量comma,計數X字符的數量

+0

你想** ** str_word_count http://us3.php.net/manual/en/function.str-word-count.php – Jakub 2010-12-15 21:24:02

回答

1

您可以使用count_chars

例從PHP手冊:

$data = "Two Ts and one F."; 
foreach (count_chars($data, 1) as $i => $val) { 
    echo "There were $val instance(s) of \"" , chr($i) , "\" in the string.\n"; 
} 

輸出(codepad):

There were 4 instance(s) of " " in the string. 
There were 1 instance(s) of "." in the string. 
There were 1 instance(s) of "F" in the string. 
There were 2 instance(s) of "T" in the string. 
There were 1 instance(s) of "a" in the string. 
There were 1 instance(s) of "d" in the string. 
There were 1 instance(s) of "e" in the string. 
There were 2 instance(s) of "n" in the string. 
There were 2 instance(s) of "o" in the string. 
There were 1 instance(s) of "s" in the string. 
There were 1 instance(s) of "w" in the string. 
+0

不要忘記檢查'isset($結果[」, '])'如果不能保證逗號在字符串中(結果數組中沒有逗號的可能性) – 2010-12-15 21:29:06