note 107336 added to function.count-chars
| From: | marcus33cz | Date: | Wed, 01 Feb 2012 11:44:01 +0000 |
| Subject: | note 107336 added to function.count-chars | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185815@lists.php.net to get a copy of this message | ||
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
/**
* Counts character occurences in a multibyte string
* @param string $input UTF-8 data
* @return array associative array of characters.
*/
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 )
?>
----
Server IP: 147.32.160.17
Probable Submitter: 109.80.205.124
----
Manual Page -- http://www.php.net/manual/en/function.count-chars.php
Edit -- https://master.php.net/note/edit/107336
Del: integrated -- https://master.php.net/note/delete/107336/integrated
Del: useless -- https://master.php.net/note/delete/107336/useless
Del: bad code -- https://master.php.net/note/delete/107336/bad+code
Del: spam -- https://master.php.net/note/delete/107336/spam
Del: non-english -- https://master.php.net/note/delete/107336/non-english
Del: in docs -- https://master.php.net/note/delete/107336/in+docs
Del: other reasons-- https://master.php.net/note/delete/107336
Reject -- https://master.php.net/note/reject/107336
Search -- https://master.php.net/manage/user-notes.php