2010-12-15 2 views
1

私はexplode()が失敗している何かを試しているので、何か他のものを試してみたい。X文字の数をカウントする

$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"; 
} 

出力:私は、文字列を持っている場合は、どのように私はあなたがPHPマニュアルからcount_chars

例を使用することができます,

+0

あなたがしたい** str_word_count ** http://us3.php.net/manual/en/function.str-word-count.php – Jakub

答えて

3

substr_count()を試してみてください。

substr_count($text, ','); 
1

のようcommaを言わせていることの文字数をカウントすることができます(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($ result ['、 ']) 'もしコンマが文字列にあるという保証がない場合(結果配列にカンマ要素がない可能性があります) –

関連する問題