Using count_chars() function we can get many information about an input string. Here is the syntax
$string : Main string to count chars
$mode : to get different return value. Default value is 0
0 Returns array of all chars and its frequency of use. Not used chars also returned with zero value (ASCII value)
1 Not used chars are not included only used chars in the string are returned with frequency of uses (ASCII value)
2 All chars with zero value ( or all chars which are not used with zero value returned )(ASCII value)
3 A string is returned with all used chars
4 A string is returned with all not used chars
Above code will return an array and by using print_r function we have displayed all keys with values of the array. The array will return all chars ( as Key of array ) and its frequency of use ( value of the key in array ) in the given string. If any char is not used then value of that key element will be zero.
With different mode values ( 0 to 4) we will get returns like this
We will try some examples here
Setting Mode to 1
By changing the mode value to 1 we will get a big list of all chars with its values. Most of them will have value as zero as we have used few chars.
Here is the code
Let us change the mode value to 3, this is the only line to be changed.
$position = count_chars($str,3);
The output is here
So you can see the above chars only we have used in our main string.
Interesting applications can be developed by using this function like checking the string for frequency of used chars. How many emails address are present in a string. By just checking frequency of the char @ ( char(64) ) we can know how many email address are included in the string. Like this many scripts can be developed.