count_chars

(PHP 4, PHP 5)

count_chars --  返回字串所用字元的訊息

描述

mixed count_chars ( string string [, int mode] )

統計 string 中每個位元組值(0..255)出現的次數,使用多種模式返回結果。可選參數 mode 預設值為 0。根據不同的 modecount_chars() 返回下列不同的結果:

  • 0 - 以所有的每個位元組值作為鍵名,出現次數作為值的陣列。

  • 1 - 與 0 相同,但只列出出現次數大於零的位元組值。

  • 2 - 與 0 相同,但只列出出現次數等於零的位元組值。

  • 3 - 返回由所有使用了的位元組值組成的字串。

  • 4 - 返回由所有未使用的位元組值組成的字串。

例子 1. count_chars() 示例

<?php

$data 
"Two Ts and one F.";

$result count_chars($data0);

for (
$i=0$i count($result); $i++) {
   if (
$result[$i] != 0)
       echo 
"There were $result[$i] instance(s) of \"" chr($i) , "\" in the string.\n";
}

?>

下邊為示例輸出:

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.

參見 strpos()substr_count()