|
|
 |
count_chars (PHP 4 , PHP 5) count_chars --
返回字符串所用字符的信息
描述mixed count_chars ( string string [, int mode])
统计 string
中每个字节值(0..255)出现的次数,使用多种模式返回结果。可选参数
mode 默认值为
0。根据不同的
mode,count_chars()
返回下列不同的结果:
0 - 以所有的每个字节值作为键名,出现次数作为值的数组。
1 - 与 0 相同,但只列出出现次数大于零的字节值。
2 - 与 0 相同,但只列出出现次数等于零的字节值。
3 - 返回由所有使用了的字节值组成的字符串。
4 - 返回由所有未使用的字节值组成的字符串。
例子 1. count_chars() 示例 |
<?php
$data = "Two Ts and one F.";
$result = count_chars($data, 0);
for ($i=0; $i < count($result); $i++) {
if ($result[$i] != 0)
echo "There were $result[$i] instance(s) of \"" , chr($i) , "\" in the string.\n";
}
?>
|
下边为示例输出:
There were 4 instance(s) of " " in the string.
There were 1 instance(s) of "." in the string.
There were 1 instance(s) of "F" in the string.
There were 2 instance(s) of "T" in the string.
There were 1 instance(s) of "a" in the string.
There were 1 instance(s) of "d" in the string.
There were 1 instance(s) of "e" in the string.
There were 2 instance(s) of "n" in the string.
There were 2 instance(s) of "o" in the string.
There were 1 instance(s) of "s" in the string.
There were 1 instance(s) of "w" in the string. |
|
参见 strpos() 和
substr_count()。
maotin at hongkong dot com
01-Feb-2001 09:04
Here are some more experiments on this relatively new and extremely handy
function.
$string = 'I have never seen ANYTHING like that before!
My number is "4670-9394".';
foreach(count_chars($string,
1) as $chr => $hit)
echo 'The character
'.chr(34).chr($chr).chr(34).' has appeared in this string '.$hit.'
times. ';
#The result looks like
#The character "
" has appeared in this string 11 times.
echo
count_chars($string,3);
#The output is
'!"-.034679AGHIMNTYabefhiklmnorstuvy'
echo strlen($string).'
is not the same as '.strlen(count_chars($string, 3));
#This shows
that '70 is not the same as 36'
As we can see above:
1)If
you cares only about what is in the string, use count_chars($string, 1) and
it will return an (associative?) array of what shows up only.
2)
Either I misunderstood what the manul actually said, or it does not work
the way it described: count_chars($strting, 3) actually returned a string
of what characters are in the string, not a string of their byte-values
(which is great because a string of numbers would be much harder to
handle);
3)This is a short version of password checking: get the
original string's length, then compare with the length of the string
returned by count_chars($string,3).
$length_of_string =
strlen($string);
$num_of_chars = strlen(count_chars($string,
3));
$diff = ($length_of_string - $num_of_chars);
if
($diff)
echo 'At least one character has been used more than
once.';
else
echo 'All character have been used only
once.;
Note that since $num_of_chars gives no information about
the actual number of occurance, we cannot go any further by the same
rationale and say when $diff =2 then 2 characters showed up twice; it might
be 1 character showd up 3 times, we have no way to tell (a good tolerance
level setter, though). You have to get the array and check the values if
you want to have more control.
4) Final trick: now we have a
primitive way to count the number of words in a string! (or do we have a
fuction for that already?)
| |