count_chars – PHP String Functions

Syntax :

string count_chars(string, mode);

Description :

count_chars() function will return information about characters used in a string, which can be string with all different characters, with all unused characters and details of ASCII values as key and number of occurrences as a value etc. Please see mode parameter for more information


Parameter :

string –  String to be checked.

mode – This is Optional parameter, It defines the return mode. 0 is the default value. Here are the return modes values which can be used

  • 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

Output :

This will return information depending upon the mode passed Or for the default mode value which is 0 (zero).


Related articles : substr_count(), str_word_count(), strlen().


Count Chars PHP Example

<?php
$strExample = "Hi from tutorialmines.net!";
echo count_chars($strExample,3);
?>

In above example ,We have a string “Hi from tutorialmines.net!”. So, when we apply count_chars() function. This will a string with all the different characters used in it.

Now Output will become –

!.Haefilmnorstu

Count Chars PHP Function Example 2
<?php
$strExample = count_chars("Hi from tutorialmines.net",4);
echo($strExample);
?>

In above example ,We have a string “Hi from tutorialmines.net!”. So, when we apply count_chars() function. This will return a string with all the unused characters .

Output will be like below:

”#$%&'()*+,-/0123456789:;<=>?@ABCDEFGIJKLMNOPQRSTUVWXYZ[\]^_`bcdghjkpqvwxyz{|}~��������������������������������������������������������������������������������������������������������������������������������

Count Chars PHP Example 3 :

In this example we will use count_chars() with mode 1 to check the string.

<?php
$str = "Hi from tutorialmines.net!";
print_r(count_chars($str,1));
?>

In above example ,We have a string “Hi from tutorialmines.net!”. So, when we apply count_chars() function. This will return an array with the ASCII value as key and how many times it occurred as value:  with all the unused characters .

Output will be like below:

Array ( [32] => 2 [33] => 1 [46] => 1 [72] => 1 [97] => 1 [101] => 2 [102] => 1 [105] => 3 [108] => 1 [109] => 2 [110] => 2 [111] => 2 [114] => 2 [115] => 1 [116] => 3 [117] => 1 )

Count Chars PHP Example 4 :
<?php
$str = "Hi from tutorialmines.net!";
$strArray = count_chars($str,1);
foreach ($strArray as $key=>$value)
{
echo "The character <b>'".chr($key)."'</b> was found $value time(s)<br>";
}
?>

In above example ,We have a string “Hi from tutorialmines.net!”. So, when we apply count_chars() function. This will return an array with the ASCII value as key and how many times it occurred as value: with all the unused characters .

Output will be like below:

The character ‘ ‘ was found 2 time(s)
The character ‘!’ was found 1 time(s)
The character ‘.’ was found 1 time(s)
The character ‘H’ was found 1 time(s)
The character ‘a’ was found 1 time(s)
The character ‘e’ was found 2 time(s)
The character ‘f’ was found 1 time(s)
The character ‘i’ was found 3 time(s)
The character ‘l’ was found 1 time(s)
The character ‘m’ was found 2 time(s)
The character ‘n’ was found 2 time(s)
The character ‘o’ was found 2 time(s)
The character ‘r’ was found 2 time(s)
The character ‘s’ was found 1 time(s)
The character ‘t’ was found 3 time(s)
The character ‘u’ was found 1 time(s)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *