count_chars Function
The count_chars() function returns how many times an ASCII character occurs within a string and returns the information.We have 4 mode values. Each of them return different results.
Syntax:
count_chars(string,mode)
Mode =>
0 - an array with the ASCII value as key and number of occurrences as value
1 - an array with the ASCII value as key and number of occurrences as value, only lists occurrences greater than zero
2 - an array with the ASCII value as key and number of occurrences as value, only lists occurrences equal to zero are listed
3 - a string with all the different characters used
4 - a string with all the unused characters
Example:
<?php
$data = “This is Sample”;
foreach (count_chars($data, 1) as $i => $val) {
echo “There were $val instance(s) of \”" , chr($i) , “\” in the string.<br>”;
}
?>
Output:
There were 2 instance(s) of ” ” in the string.
There were 1 instance(s) of “S” in the string.
There were 1 instance(s) of “T” in the string.
There were 1 instance(s) of “a” in the string.
There were 1 instance(s) of “e” in the string.
There were 1 instance(s) of “h” in the string.
There were 2 instance(s) of “i” in the string.
There were 1 instance(s) of “l” in the string.
There were 1 instance(s) of “m” in the string.
There were 1 instance(s) of “p” in the string.
There were 2 instance(s) of “s” in the string.
