note 76916 deleted from function.ord by danbrown
| From: | danbrown@php.net | Date: | Tue, 11 Oct 2011 16:07:15 +0000 |
| Subject: | note 76916 deleted from function.ord by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-183798@lists.php.net to get a copy of this message | ||
Note Submitter: steph at zend dot com
----
An update for ng4rrjanbiah's uniord() in #46267. Again, no mbstring requirement - this is just
a code cleanup. Longer, but prettier - I think, anyway ;)
<?php
function uniord($ch) {
$n = ord($ch{0});
if ($n < 128) {
return $n; // no conversion required
}
if ($n < 192 || $n > 253) {
return false; // bad first byte || out of range
}
$arr = array(1 => 192, // byte position => range from
2 => 224,
3 => 240,
4 => 248,
5 => 252,
);
foreach ($arr as $key => $val) {
if ($n >= $val) { // add byte to the 'char' array
$char[] = ord($ch{$key}) - 128;
$range = $val;
} else {
break; // save some e-trees
}
}
$retval = ($n - $range) * pow(64, sizeof($char));
foreach ($char as $key => $val) {
$pow = sizeof($char) - ($key + 1); // invert key
$retval += $val * pow(64, $pow); // dark magic
}
return $retval;
}
?>