Home >Backend Development >PHP Tutorial >How to use the count_chars function in php
Usage of count_chars function in php: [count_chars(string,mode)]. The count_chars function is used to return information about the characters used in a string, such as [count_chars($str,3)].
php The count_chars function returns a string that contains all the different characters used in the string. Its syntax is count_chars(string,mode), parameter string Required, specifies the string to be checked.
(Recommended tutorial: php video tutorial)
How to use the php count_chars function?
Function: Returns a string containing all different characters used in the string.
Syntax:
count_chars(string,mode)
Parameters:
string Required. Specifies the string to check.
mode Optional. Specifies the return mode. The default is 0.
The following are different return modes:
0 - Array, the ASCII value is the key name, the number of occurrences is the key value,
1 - Array, the ASCII value is the key name, the number of occurrences is the key value, only the values whose occurrence number is greater than 0 are listed,
2 - Array, the ASCII value is the key name, the number of occurrences is the key value, only the values are listed A value with an occurrence count equal to 0,
3 - a string with all used distinct characters,
4 - a string with all unused distinct characters .
Description: Return information about the characters used in the string
php count_chars() function usage example 1
<?php //使用模式3输出 $str = "Hello php.cn!"; echo count_chars($str,3); ?>
Output:
!.Hcehlnop
php count_chars() function usage example 2
<?php //使用模式1的数组形式输出 $str = "Hello php.cn!"; print_r(count_chars($str,1)) ; ?>
Output:
Array ( [32] => 1 [33] => 1 [46] => 1 [72] => 1 [99] => 1 [101] => 1 [104] => 1 [108] => 2 [110] => 1 [111] => 1 [112] => 2 )
The above is the detailed content of How to use the count_chars function in php. For more information, please follow other related articles on the PHP Chinese website!