Returns a string containing all the distinct characters used in "Hello World!" (mode 3):
<?php$str = "Hello World!";echo count_chars($str,3);?>The count_chars() function returns information about the characters used in a string (for example, the number of times an ASCII character appears in a string, or whether a character has already been used in a string).
count_chars( string,mode )
parameter | describe |
---|---|
string | Required. Specifies the string to check. |
mode | Optional. Specifies the return mode. The default is 0. There are different return modes: 0 - Array, ASCII value is the key name, 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, the ASCII value is the key name, the number of occurrences is the key value, only the values whose occurrence number is equal to 0 are listed 3 - String with all different characters used 4 - String with all unused distinct characters |
Return value: | Depends on the specified mode parameter. |
---|---|
PHP version: | 4+ |
Return a string containing all characters not used in "Hello World!" (mode 4):
<?php$str = "Hello World!";echo count_chars($str,4);?>In this example, we will use count_chars() to check a 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 as the key value:
<?php$str = "Hello World!";print_r(count_chars($str,1));?>Another example of counting the number of times ASCII characters appear in a 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>"; }?>