©
This document uses PHP Chinese website manual Release
(PHP 4, PHP 5)
count_chars — 返回字符串所用字符的信息
$string
[, int $mode
= 0
] )
统计 string
中每个字节值(0..255)出现的次数,使用多种模式返回结果。
string
需要统计的字符串。
mode
参见返回的值。
根据不同的 mode
, count_chars()
返回下列不同的结果:
Example #1 count_chars() 示例
<?php
$data = "Two Ts and one F." ;
foreach ( count_chars ( $data , 1 ) as $i => $val ) {
echo "There were $val 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.
[#1] qeremy [atta] gmail [dotta] com [2013-01-19 18:06:19]
Another approach to counting unicode chars.
<?php
function count_chars_unicode($str, $x = false) {
$tmp = preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);
foreach ($tmp as $c) {
$chr[$c] = isset($chr[$c]) ? $chr[$c] + 1 : 1;
}
return is_bool($x)
? ($x ? $chr : count($chr))
: $chr[$x];
}
$str = "?eker ?eker y?riiiiiiiiiimmmmm";
print_r(count_chars_unicode($str, '?')); // frequency of "?"
print_r(count_chars_unicode($str)); // count of uniq chars
print_r(count_chars_unicode($str, true)); // all chars with own frequency
?>
Outputs;
1
9
Array
(
[?] => 2
[e] => 4
[k] => 2
[r] => 3
[ ] => 2
[y] => 1
[?] => 1
[i] => 10
[m] => 5
)
[#2] marcus33cz [2012-02-01 11:43:59]
If you have problems using count_chars with a multibyte string, you can change the page encoding. Alternatively, you can also use this mb_count_chars version of the function. Basically it is mode "1" of the original function.
<?php
function mb_count_chars($input) {
$l = mb_strlen($input, 'UTF-8');
$unique = array();
for($i = 0; $i < $l; $i++) {
$char = mb_substr($input, $i, 1, 'UTF-8');
if(!array_key_exists($char, $unique))
$unique[$char] = 0;
$unique[$char]++;
}
return $unique;
}
$input = "Let's try some Greek letters: ???????????????????????, Russian: ????????????, Czech: ?????????????";
print_r( mb_count_chars($input) );
//returns: Array ( [L] => 1 [e] => 7 [t] => 4 ['] => 1 [s] => 5 [ ] => 9 [r] => 3 [y] => 1 [o] => 1 [m] => 1 [G] => 1 [k] => 1 [l] => 1 [:] => 3 [??] => 5 [??] => 1 [??] => 1 [??] => 1 [??] => 1 [??] => 1 [??] => 1 [??] => 1 [??] => 1 [,] => 2 [R] => 1 [u] => 1 [i] => 1 [a] => 1 [n] => 1 [??] => 2 [??] => 2 [??] => 1 [??] => 1 [C] => 1 [z] => 1 [c] => 1 [h] => 1 [??] => 1 [?] => 1 [?] => 1 [?] => 1 [?] => 1 [?] => 1 [??] => 1 [??] => 1 [??] => 1 )
?>
[#3] phpC2007 [2007-12-04 07:42:21]
Here's a function to count number of strings in a string. It can be used as a simple utf8-enabled count_chars (but limited to a single mode)...
<?php
function utf8_count_strings($stringChar)
{
$num = -1;
$lenStringChar = strlen($stringChar);
for ($lastPosition = 0;
$lastPosition !== false;
$lastPosition = strpos($textSnippet, $stringChar, $lastPosition + $lenStringChar))
{
$num++;
}
return $num;
}
?>
[#4] pzb at novell dot com [2007-07-28 18:29:57]
This function is great for input validation. I frequently need to check that all characters in a string are 7-bit ASCII (and not null). This is the fastest function I have found yet:
<?php
function is7bit($string) {
// empty strings are 7-bit clean
if (!strlen($string)) {
return true;
}
// count_chars returns the characters in ascending octet order
$str = count_chars($str, 3);
// Check for null character
if (!ord($str[0])) {
return false;
}
// Check for 8-bit character
if (ord($str[strlen($str)-1]) & 128) {
return false;
}
return true;
}
?>
[#5] apinpratap at gmail dot com [2007-03-29 22:54:27]
this code can find each characters count
<?php
$enter = 0;
$data = strtolower ($inputString);
foreach (count_chars ($data, 1) as $i => $val)
{
if ($enter == 1)
{
$enter = 0;
continue;
}
if (chr ($i) == "\n")
{
echo "There are $val instance(s) of \" Enter \" in the string.\n";
$enter = 1;
}
else
{
echo "There are $val instance(s) of \"" , chr ($i) , "\" in the string.\n";
}
}
?>
[#6] Eric Pecoraro [2005-05-27 07:31:10]
<?php
// Require (n) unique characters in a string
// Modification of a function below which ads some flexibility in how many unique characters are required in a given string.
$pass = '123456' ; // true
$pass = '111222' ; // false
req_unique($pass,3);
function req_unique($string,$unique=3) {
if ( count(count_chars($string,1)) < $unique) {
echo 'false';
}else{
echo 'true';
}
}
?>
[#7] seb at synchrocide dot net [2004-05-16 15:08:21]
After much trial and error trying to create a function that finds the number of unique characters in a string I same across count_chars() - my 20+ lines of useless code were wiped for this:
<?php
function unichar($string) {
$two= strtolower(str_replace(' ', '', $string));
$res = count(count_chars($two, 1));
return $res;
}
echo unichar("bob"); // 2
echo unichar("Invisibility"); //8
echo unichar("The quick brown fox slyly jumped over the lazy dog"); //26
?>
I have no idea where this could be used, but it's quite fun
[#8] mlong at mlong dot org [2002-01-29 16:27:10]
// Usefulness of the two functions
<?php
$string="aaabbc";
// You just want to count the letter a
$acount=substr_count($string,"a");
// You want to count both letter a and letter b
$counts=count_chars($string,0);
$acount=$counts[ord("a")];
$bcount=$counts[ord("b")];
?>
[#9] maotin at hongkong dot com [2001-01-31 17:04:31]
Here are some more experiments on this relatively new and extremely handy function.
<?php
$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.<BR>';
#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).
<?php
$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?)