PHP count_chars() Function

PHP count_chars() function returns information about characters used in a string.

Syntax

mixed count_chars(str,mode);

count_chars() Function Parameter

ParameterDescription
str :Required parameter. The examined string.
mode :Optional parameter. Returns modes. 0 is default. different modes are following:

  • 0 - an array with the ASCII value as key and number of occurrences as value
  • 1 - same as 0 but only byte-values with a frequency greater than zero are listed.
  • 2 - same as 0 but only byte-values with a frequency equal to zero are listed.
  • 3 - a string containing all unique characters is returned.
  • 4 - a string containing all not used characters is returned.

count_chars() Function Return Value

Return Value :Returns one of the mode mentioned above.

count_chars() Function Example 1

<?php
echo count_chars("Returns one of the mode mentioned above",7);
?>

count_chars() Function Example 2

<?php
$strArray = count_chars("Returns one of the mode mentioned above",1);

foreach ($strArray as $key=>$val)
{
 echo "There were $val instance(s) of \"" , chr($i) , "\" in the string.\n";
}
  
?>