实例
返回一个字符串,包含所有在 "Hello World!" 中使用过的不同字符(模式 3):
<?php
$str = "Hello World!";
echo count_chars($str,3);
?>
输出
!HWdelor
统计 ASCII 字符在字符串中出现的次数另一个实例:
<?php
$str = "PHP is pretty fun!!";
$strArray = count_chars($str,1);
foreach ($strArray as $key=>$value)
{
echo "The character <b>'".chr($key)."'</b> was found $value time(s)<br>";
}
?>
输出
The character ' ' was found 3 time(s)
The character '!' was found 2 time(s)
The character 'H' was found 1 time(s)
The character 'P' was found 2 time(s)
The character 'e' was found 1 time(s)
The character 'f' was found 1 time(s)
The character 'i' was found 1 time(s)
The character 'n' was found 1 time(s)
The character 'p' was found 1 time(s)
The character 'r' was found 1 time(s)
The character 's' was found 1 time(s)
The character 't' was found 2 time(s)
The character 'u' was found 1 time(s)
The character 'y' was found 1 time(s)
定义和用法
count_chars() 函数返回字符串所用字符的信息(例如,ASCII 字符在字符串中出现的次数,或者某个字符是否已经在字符串中使用过)。
语法
count_chars(string,mode)
参数 | 描述 |
---|---|
string | 必需。规定要检查的字符串。 |
mode | 可选。规定返回模式。默认是 0。有以下不同的返回模式:
|