Reputation: 21509
I have a string like this:
$strUTF8 = 'ABCDAÄ';
How can I get the count of each character and its hex value ?
Example:
\x41 = 2 [A]
\x42 = 1 [B]
...
\xC3\x84 = 1 [Ä]
Upvotes: 1
Views: 1562
Reputation: 569
If you are only checking against UTF-8, you could use mb_strlen
.
http://www.php.net/manual/de/function.mb-strlen.php
$strUTF8 = 'ABCDAÄ';
var_dump(mb_strlen($strUTF8, 'UTF-8')); // 6
To get all the ordinal values of your characters, iterare through the string and print the chars with mb_substr
.
$strUTF8 = 'ABCDAÄ';
$len = mb_strlen($strUTF8, 'UTF-8');
for ($i = 0; $i < $len; $i++) {
$chr = mb_substr($strUTF8, $i, 1, 'UTF-8');
var_dump($chr, ord($chr));
}
Upvotes: 2
Reputation: 318698
for($i = 0, $n = mb_strlen($strUTF8); $i < $n; $i++) {
$mbchar = mb_substr($strUTF8, $i, 1);
$numChars = strlen($mbchar);
for($j = 0; $j < $numChars; $j++) {
printf('%02x', ord($mbchar[$j]));
}
echo "\n";
}
Upvotes: 3