note 111144 added to function.count-chars
| From: | qeremy | Date: | Sat, 19 Jan 2013 18:06:21 +0000 |
| Subject: | note 111144 added to function.count-chars | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-193271@lists.php.net to get a copy of this message | ||
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
)
----
Server IP: 95.130.168.26
Probable Submitter: 88.253.79.190
----
Manual Page -- http://php.net/manual/en/function.count-chars.php
Edit -- https://master.php.net/note/edit/111144
Del: integrated -- https://master.php.net/note/delete/111144/integrated
Del: useless -- https://master.php.net/note/delete/111144/useless
Del: bad code -- https://master.php.net/note/delete/111144/bad+code
Del: spam -- https://master.php.net/note/delete/111144/spam
Del: non-english -- https://master.php.net/note/delete/111144/non-english
Del: in docs -- https://master.php.net/note/delete/111144/in+docs
Del: other reasons-- https://master.php.net/note/delete/111144
Reject -- https://master.php.net/note/reject/111144
Search -- https://master.php.net/manage/user-notes.php