PHP count_chars()function

PHP count_chars() is most important string function. It is used to return information about characters in a string.

Syntax:

ParameterDescriptionRequired/Optional
stringThe string to be checkedRequired
modeSpecify the return modesOptional

Note: Depending on mode 'count_chars()' function returns one of the following:

  • 0 : It is an array with the byte-value as key and the frequency of every byte as value.
  • 1 : It is same as 0 but only byte-value 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.

Example 1

Output:

Your given string: Hello World!
By using 'count_chars()' function your string is : !HWdelor

Example 2

Output:

Array ( [32] => 1 [33] => 1 [72] => 1 [87] => 1 [100] => 1 [101] => 1 [108] => 3 [111] => 2 [114] => 1 )

Example 3

Output:

The character ' ' was found 3 time(s)
The character '!' was found 1 time(s)
The character 'H' was found 1 time(s)
The character 'L' was found 2 time(s)
The character 'P' was found 2 time(s)
The character 'a' was found 2 time(s)
The character 'e' was found 2 time(s)
The character 'g' was found 2 time(s)
The character 'i' was found 1 time(s)
The character 'l' was found 1 time(s)
The character 'n' was found 1 time(s)
The character 'o' was found 1 time(s)
The character 's' was found 1 time(s)
The character 'u' was found 1 time(s)
The character 'v' was found 1 time(s)
The character 'y' was found 1 time(s)

Next TopicPHP String




Latest Courses