note 100464 deleted from function.ord by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 Oct 2010 14:34:01 +0000 |
| Subject: | note 100464 deleted from function.ord by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173246@lists.php.net to get a copy of this message | ||
Note Submitter: swinefeaster /at\ (g)mail d0t com
----
Thanks to Kerry for the ordUTF8 function. To make it even clearer, here's a function that calls
it and iterates through a UTF-8 string and spits out a unicode value for each decoded character of
the string:
function echoUnicodeCharsForUtf8String($input)
{
$output = "";
$len = strlen($input);
while($i < $len)
{
$character = substr($input, $i, 1);
$charNumBytes = 1;
$attempt = ordUTF8($input, $i, $charNumBytes);
echo "unicodeChar=" . $attempt . "<p>";//TODO
$i += $charNumBytes;
}
return $output;
}
// Kerry's function verbatim
function ordUTF8($c, $index = 0, &$bytes = null)
{
$len = strlen($c);
$bytes = 0;
if ($index >= $len)
return false;
$h = ord($c{$index});
if ($h <= 0x7F) {
$bytes = 1;
return $h;
}
else if ($h < 0xC2)
return false;
else if ($h <= 0xDF && $index < $len - 1) {
$bytes = 2;
return ($h & 0x1F) << 6 | (ord($c{$index + 1}) & 0x3F);
}
else if ($h <= 0xEF && $index < $len - 2) {
$bytes = 3;
return ($h & 0x0F) << 12 | (ord($c{$index + 1}) & 0x3F) << 6
| (ord($c{$index + 2}) & 0x3F);
}
else if ($h <= 0xF4 && $index < $len - 3) {
$bytes = 4;
return ($h & 0x0F) << 18 | (ord($c{$index + 1}) & 0x3F) << 12
| (ord($c{$index + 2}) & 0x3F) << 6
| (ord($c{$index + 3}) & 0x3F);
}
else
return false;
}
Check out CueBrain for learning languages on Android!