Current Location: Home> Function Categories> count_chars

count_chars

Returns information about the characters used in the string - counts the number of times each byte value (0..255) occurs in a string
Name:count_chars
Category:String
Programming Language:php
One-line Description:Returns information about the characters used in the string.

Definition and usage

count_chars() function returns information about the characters used in the string (for example, the number of times an ASCII character appears in the string, or whether a character has been used in the string).

Example

Example 1

Returns a string containing all the different characters used in "Hello World!" (Mode 3):

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

Try it yourself

Example 2

Returns a string containing all characters that were not used in "Hello World!" (Schema 4):

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

Try it yourself

Example 3

In this example, we will use count_chars() to check the string, with the return mode set to 1. Mode 1 will return an array with the ASCII value as the key name and the number of occurrences is the key value:

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

Try it yourself

Example 4

Another instance of counting the number of times an ASCII character appears in a string:

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

foreach ( $strArray as $key => $value )
  {
echo "character<b>'" . chr ( $key ) . "'</b> is found $value times.<br>" ;
  }
?>

Try it yourself

grammar

 count_chars ( string , mode )
parameter describe
string Required. Specifies the string to be checked.
mode

Optional. Specify the return mode. The default is 0. The following are the different return modes:

  • 0 - Array, ASCII value is the key name, and the number of occurrences is the key value
  • 1 - Array, ASCII value is the key name, the number of occurrences is the key value, only values ​​with occurrences greater than 0 are listed
  • 2 - Array, ASCII value is the key name, the number of occurrences is the key value, only the values ​​with the number of occurrences equal to 0 are listed
  • 3 - String with all used different characters
  • 4 - String with all unused different characters
Similar Functions
Popular Articles