Computer >> Computer tutorials >  >> Programming >> PHP

count_chars() function in PHP


The count_charss() method is used to returns the information about character used in a string

Syntax

cont_chars(str, mode)

Parameters

  • str − String to be checked.

  • mode − Specifies the return modes. 0 is default.

  • The possible return modes are −

    • 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

Return

The count_chars() function returns depending on the specified mode parameter.

The following is an example −

Example

<?php
$s = "Weclome!";
print_r(count_chars($s,1));
?>

The following is the output −

Output

Array
(
   [33] => 1
   [87] => 1
   [99] => 1
   [101] => 2
   [108] => 1
   [109] => 1
   [111] => 1
)

Let us see another example −

Example

<?php
$s = "Welcome!";
echo count_chars($s,3);
?>

The following is the output −

Output

!Wcelmo