PHP count_chars() Function
Example
Return a string containing all the different characters used in "Hello World!" (mode 3):
Run Example »
Definition and Usage
The count_chars() function returns information about the characters used in a string (for example, how many times an ASCII character appears in the string, or whether a character has been used in the string).
Syntax
count_chars(
string,mode
)
| Parameters | Description |
|---|---|
| string | Required. Specifies the string to check. |
| mode | Optional. Specifies the return mode. Default is 0. The following different return modes are available:
|
Technical Details
| Return Value: | Depending on the specified mode parameter was added. |
|---|---|
| PHP Version: | 4+ |
More examples
Example 1
Return a string containing all the characters not used in "Hello World!" (mode 4):
Run Example »
Example 2
In this example, we will use count_chars() to inspect the string, with the return mode set to 1. Mode 1 returns an array with ASCII values as keys and occurrence counts as values:
Run Example »
Example 3
Another example of counting how many times ASCII characters appear in a string:
$value) { echo "The character '".chr($key)."' was found $value time(s)"; } ?>
Run Example »
PHP String Reference Manual