当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > count_chars() 函数
The count_chars() function returns how many times an ASCII character occurs within a string and returns the information.
count_chars()函数的作用是:返回字符串所使用字符的信息的次数。
count_chars(string,mode) |
Parameter参数 | Description描述 |
---|---|
string | Required. The string to be checked 必要参数。指定需要被检查的字符串 |
mode | Optional. Specifies the return modes. 0 is default. The different return modes are: 可选参数。指定需要返回的模式。具体如下(默认值是0):
|
In this example we will use count_char() with mode 1 to check the string. Mode 1 will return an array with the ASCII value as key and how
many times it occurred as value (e.g. in the example below, the ASCII value for the letter "l" is 108, and it occurs three times):
在下面的案例中,我们将使用count_char()函数,选择mode 1来检查字符串。Mode 1将以ASCII值为键,以事件数为值的一个数组(如:在下面的案例中,"l"所对应的ASCII值是108,它出现了3次):
<?php $str = "Hello World!"; print_r(count_chars($str,1)); ?> |
The output of the code above will be:
上述代码将输出下面的结果:
Array ( [32] => 1 [33] => 1 [72] => 1 [87] => 1 [100] => 1 [101] => 1 [108] => 3 [111] => 2 [114] => 1 ) |
In this example we will use the count_char() with mode 3 to check the string. Mode 3 will return a string with all the different characters used:
在下面的案例中,我们将使用count_char()函数,选择mode 3来检查字符串。Mode 3将返回一个包含不同字符的字符串:
<?php $str = "Hello World!"; echo count_chars($str,3); ?> |
The output of the code above will be:
上述代码将输出下面的结果:
!HWdelor |