PHP count_chars() Function

P
PHP convert_uuencode() Function
PHP crc32() Function

In this article, you will learn how to get information about the characters in a string. The PHP count_chars() function returns the information about the string characters.

For example, if you want to count of character appears in a string, or to check if a character exist or not, the count_chars() function return this information.

What is the syntax of the COUNT_CHARS() function in php?

count_chars(string,mode)
ParameterDescription
stringThe string to get the information from – Required
modeThere are different modes of output. These modes are explained following.
0 – It returns an array that contains ASCII values as keys and their occurrence as values.
1It returns an array that contains ASCII values as keys and their occurrences as values (occurrences only greater than 0).
2It returns an array that contains ASCII values as keys and their occurrences as values (occurrences only equal to 0).
3 – String containing the strings used.
4 – String containing the strings unused.
PHP COUNT_CHARS() method

Examples of the COUNT_CHARS() function

Example 1. In this example, we returned all the unused characters in the given string.

<?php
$str = "Hello World!";
echo count_chars($str,4);
?>

Example 2. In this example, we use the count_char function with mode 1 that returns an array that contains ASCII values as keys and their occurrence as values.

<?php
$str = "Hello World!";
print_r(count_chars($str,1));
?>

Example 3. In this example, we count the number of ASCII characters in the string.

<?php
$str = "PHP is pretty fun!!";
$strArray = count_chars($str,1);

foreach ($strArray as $key=>$value)
  {
echo "The character <b>'".chr($key)."'</b> was found $value time(s)<br>";
  }
?>
PHP convert_uuencode() Function
PHP crc32() Function

Add comment

Tutor Network

Share this

Learn PHP from A to Z