$str = "Welcome to plus2net PHP section";
$position = count_chars($str,3);
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 chars
$mode : to get different return value. Default value is 0

$mode values

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 
$str = "Welcome to plus2net PHP section";
$position = count_chars($str,2);
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
$str = " Welcome to plus2net PHP section";
$position = count_chars($str,1);
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
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.

Here is one example
$str = ",,";
$position = count_chars($str,1);
echo $position[64]; // output is 3

String Functions str_word_count() Sub string count
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer