本文翻译自www.php.net。
本函数适用于PHP4.0b4以上版本。
函数名称:count_chars -- 返回字符串中的字符使用信息。
简介:
mixed count_chars (string string [, mode])
统计[0..255]字符在字符串中出现的频率,并以多种方式返回结果。默认模式为模式0,下面是各种模式的说明:
模式0:以数组方式返回字符出现频率
模式1:和模式0相同,但只列出频率大于零的结果
模式2:和模式0相同,但只列出频率等于零的结果
模式3:返回该字符串中所有使用的字符的出现频率
模式4:返回一个字符串,这个字符串包含所有未出现的字符
例子:
shaun@phpboards.com
08-Sep-2000 02:09
Usage Example
Using count_chars() to figure out which characters are in a string, and how many instances of each character were present:
$data = "Two Ts and one F.";
$result = (count_chars($data, 0));
for ($i=0; $i
if ($result[$i])
echo "There were $result[$i] instances of "" . (chr($i)) ."" in the string.
";
}
}
?>
The results would be:
There were 4 instances of " " in the string.
There were 1 instances of "." in the string.
There were 1 instances of "F" in the string.
There were 2 instances of "T" in the string.
There were 1 instances of "a" in the string.
There were 1 instances of "d" in the string.
There were 1 instances of "e" in the string.
There were 2 instances of "n" in the string.
There were 2 instances of "o" in the string.
There were 1 instances of "s" in the string.
There were 1 instances of "w" in the string.
Application Example
Using count_chars() to perform one check on the contents of a potential password string: if any character appears more than 5 times in the string, reject the password as unsafe.
# $password is passed to the script on runtime
$result = (count_chars($password, 0));
for ($i=0; $i
if ($result[$i] > 5)
die("Your password contained too many instances of the same
character. Please go back and choose a more random password.");
}
?>
Hope these help!