note 78032 added to function.ord
| From: | kerry at shetline dot com | Date: | Tue, 25 Sep 2007 03:04:15 +0000 |
| Subject: | note 78032 added to function.ord | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130767@lists.php.net to get a copy of this message | ||
Here's my take on an earlier-posted UTF-8 version of ord, suitable for iterating through a
string by Unicode value. The function can optionally take an index into a string, and optionally
return the number of bytes consumed by a character so that you know how much to increment the index
to get to the next character.
<?php
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;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 71.168.66.105
----
Manual Page -- http://www.php.net/manual/en/function.ord.php
Edit -- https://master.php.net/note/edit/78032
Del: integrated -- https://master.php.net/note/delete/78032/integrated
Del: useless -- https://master.php.net/note/delete/78032/useless
Del: bad code -- https://master.php.net/note/delete/78032/bad+code
Del: spam -- https://master.php.net/note/delete/78032/spam
Del: non-english -- https://master.php.net/note/delete/78032/non-english
Del: in docs -- https://master.php.net/note/delete/78032/in+docs
Del: other reasons-- https://master.php.net/note/delete/78032
Reject -- https://master.php.net/note/reject/78032
Search -- https://master.php.net/manage/user-notes.php