note 77905 added to function.ord
| From: | MiguelPerez at osu1 dot php dot net | Date: | Wed, 19 Sep 2007 11:57:06 +0000 |
| Subject: | note 77905 added to function.ord | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130591@lists.php.net to get a copy of this message | ||
The following uniord function is simpler and more efficient than any of the ones suggested without
depending on mbstring or iconv. It's also more validating (code points above U+10FFFF are
invalid; sequences starting with 0xC0 and 0xC1 are invalid overlong encodings of characters below
U+0080), though not entirely validating, so it still assumes proper input.
<?php
function uniord($c) {
$h = ord($c{0});
if ($h <= 0x7F) {
return $h;
} else if ($h < 0xC2) {
return false;
} else if ($h <= 0xDF) {
return ($h & 0x1F) << 6 | (ord($c{1}) & 0x3F);
} else if ($h <= 0xEF) {
return ($h & 0x0F) << 12 | (ord($c{1}) & 0x3F) << 6
| (ord($c{2}) & 0x3F);
} else if ($h <= 0xF4) {
return ($h & 0x0F) << 18 | (ord($c{1}) & 0x3F) << 12
| (ord($c{2}) & 0x3F) << 6
| (ord($c{3}) & 0x3F);
} else {
return false;
}
}
?>
If you want a fully validating function, you'll need to check the extra octets are between 0x80
and 0xBF, and there are no overlong encodings (characters below a certain point in n-octet
sequences).
----
Server IP: 80.64.47.13
Probable Submitter: 217.125.117.197
----
Manual Page -- http://www.php.net/manual/en/function.ord.php
Edit -- https://master.php.net/note/edit/77905
Del: integrated -- https://master.php.net/note/delete/77905/integrated
Del: useless -- https://master.php.net/note/delete/77905/useless
Del: bad code -- https://master.php.net/note/delete/77905/bad+code
Del: spam -- https://master.php.net/note/delete/77905/spam
Del: non-english -- https://master.php.net/note/delete/77905/non-english
Del: in docs -- https://master.php.net/note/delete/77905/in+docs
Del: other reasons-- https://master.php.net/note/delete/77905
Reject -- https://master.php.net/note/reject/77905
Search -- https://master.php.net/manage/user-notes.php