Latest web development tutorials

PHP count_chars () function

PHP String Reference PHP String Reference

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);
?>

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。有以下不同的返回模式:
  • 0 - 数组,ASCII 值为键名,出现的次数为键值
  • 1 - 数组,ASCII 值为键名,出现的次数为键值,只列出出现次数大于 0 的值
  • 2 - 数组,ASCII 值为键名,出现的次数为键值,只列出出现次数等于 0 的值
  • 3 - 字符串,带有所有使用过的不同的字符
  • 4 - 字符串,带有所有未使用过的不同的字符

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);
?>

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));
?>

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>";
}
?>

Running instance »


PHP String Reference PHP String Reference