count_chars
count_chars
(PHP 4, PHP 5, PHP 7)
count_chars - 返回有关字符串中使用的字符的信息
描述
mixed count_chars ( string $string [, int $mode = 0 ] )
计算字符串中每个字节值(0..255)的出现次数,并以各种方式返回它。
参数
string
被检查的字符串。
mode
查看返回值。
返回值
根据mode
count_chars()
返回以下值之一:
- 0 - 一个数组,其中字节值为键,每个字节的频率为值。
- 1 - 与0相同,但仅列出频率大于零的字节值。
- 2 - 与0相同,但仅列出频率等于零的字节值。
- 3 - 返回包含所有唯一字符的字符串。
- 4 - 返回包含所有未使用字符的字符串。
例子
示例#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";
}
?>
上面的例子将输出:
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() - 计算子串出现次数
← convert_uuencode
crc32 →