$str = "Welcome to plus2net PHP section";
$position = count_chars($str,3);
print_r($position);
Output
2HPWceilmnopstu
Using count_chars() function we can get many information about an input string. Here is the syntax
Count_chars($string, $mode)
$string : Main string to count chars0 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
$str = "Welcome to plus2net PHP section";
$position = count_chars($str,2);
print_r($position);
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
$str = " Welcome to plus2net PHP section";
$position = count_chars($str,1);
print_r($position);
The output is here
Array ( [32] => 5 [50] => 1 [72] => 1 [80] => 2 [87] => 1 [99] => 2 [101] => 4 [105] => 1 [108] => 2
[109] => 1 [110] => 2 [111] => 3 [112] => 1 [115] => 2 [116] => 3 [117] => 1 )
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
2HPWceilmnopstu
So you can see the above chars only we have used in our main string. $str = " ron@domain.com, kris@example.com, benoy@testing.com";
$position = count_chars($str,1);
echo $position[64]; // output is 3
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.