字符串 函数
PHP Manual

count_chars

(PHP 4, PHP 5, PHP 7)

count_chars返回字符串所用字符的信息

说明

mixed count_chars ( string $string [, int $mode = 0 ] )

统计 string 中每个字节值(0..255)出现的次数,使用多种模式返回结果。

参数

string

需要统计的字符串。

mode

参见返回的值。

返回值

根据不同的 modecount_chars() 返回下列不同的结果:

范例

Example #1 count_chars() 示例

<?php
$data 
"Two Ts and one F.";

foreach (
count_chars($data1) as $i => $val) {
   echo 
"There were $val 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.

参见


字符串 函数
PHP Manual