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).
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
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
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
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
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:
|