PHP count_chars () function
Examples
Returns a string containing all used in the "Hello World!" In too different character (Mode 3):
<?php
$str = "Hello World!";
echo count_chars($str,3);
?>
$str = "Hello World!";
echo count_chars($str,3);
?>
Running instance »
Definition and Usage
count_chars () function returns the character string information (for example, the number of ASCII characters appear in the string, or whether a character had been used in the string).
grammar
count_chars( string,mode )
参数 | 描述 |
---|---|
string | 必需。规定要检查的字符串。 |
mode | 可选。规定返回模式。默认是 0。有以下不同的返回模式:
|
technical details
return value: | Depending on the mode specified parameters. |
---|---|
PHP version: | 4+ |
More examples
Example 1
Returns a string that contains all the "Hello World!" In unused character (mode 4):
<?php
$str = "Hello World!";
echo count_chars($str,4);
?>
$str = "Hello World!";
echo count_chars($str,4);
?>
Running instance »
Example 2
In this example, we will use count_chars () to check the string and returns mode is set to 1. Mode 1 will return an array, ASCII value of the key name, the number of occurrences of key:
<?php
$str = "Hello World!";
print_r(count_chars($str,1));
?>
$str = "Hello World!";
print_r(count_chars($str,1));
?>
Running instance »
Example 3
Frequency statistics ASCII character appears in a string another example:
<?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>";
}
?>
$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>";
}
?>
Running instance »
PHP String Reference