Read more: PHP function chr of unicode character
We have 6 methods:
<?php /** * @author www.Tutorialspots.com * @copyright 2015 */ function uniord($c) { $t = mb_convert_encoding($c, 'UCS-2LE', 'UTF-8'); return (ord($t{1}) << 8) + ord($t{0}); } function uniord2($c) { $ord0 = ord($c{0}); if ($ord0>=0 && $ord0<=127) return $ord0; $ord1 = ord($c{1}); if ($ord0>=192 && $ord0<=223) return ($ord0-192)*64 + ($ord1-128); $ord2 = ord($c{2}); if ($ord0>=224 && $ord0<=239) return ($ord0-224)*4096 + ($ord1-128)*64 + ($ord2-128); $ord3 = ord($c{3}); if ($ord0>=240 && $ord0<=247) return ($ord0-240)*262144 + ($ord1-128)*4096 + ($ord2-128)*64 + ($ord3-128); return false; } function uniord3($u) { $k = mb_convert_encoding($u, 'UCS-2LE', 'UTF-8'); $k1 = ord(substr($k, 0, 1)); $k2 = ord(substr($k, 1, 1)); return $k2 * 256 + $k1; } function uniord4($string, &$offset=0) { $code = ord(substr($string, $offset,1)); if ($code >= 128) { //otherwise 0xxxxxxx if ($code < 224) $bytesnumber = 2; //110xxxxx else if ($code < 240) $bytesnumber = 3; //1110xxxx else if ($code < 248) $bytesnumber = 4; //11110xxx $codetemp = $code - 192 - ($bytesnumber > 2 ? 32 : 0) - ($bytesnumber > 3 ? 16 : 0); for ($i = 2; $i <= $bytesnumber; $i++) { $offset ++; $code2 = ord(substr($string, $offset, 1)) - 128; //10xxxxxx $codetemp = $codetemp*64 + $code2; } $code = $codetemp; } $offset += 1; if ($offset >= strlen($string)) $offset = -1; return $code; } function uniord5($c) { $ud = 0; if (ord($c{0})>=0 && ord($c{0})<=127) $ud = ord($c{0}); if (ord($c{0})>=192 && ord($c{0})<=223) $ud = (ord($c{0})-192)*64 + (ord($c{1})-128); if (ord($c{0})>=224 && ord($c{0})<=239) $ud = (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128); if (ord($c{0})>=240 && ord($c{0})<=247) $ud = (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128); if (ord($c{0})>=248 && ord($c{0})<=251) $ud = (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128); if (ord($c{0})>=252 && ord($c{0})<=253) $ud = (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128); //error if (ord($c{0})>=254 && ord($c{0})<=255) $ud = false; return $ud; } function uniord6($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; }?>
Example:
echo uniord("♥"); //9829
echo "
";
echo uniord2("❤"); //10084
echo "
";
echo uniord3("❥"); //10085
echo "
";
echo uniord4("❦"); //10086
echo "
";
echo uniord5("❧"); //10087
echo "
";
echo uniord6("❧"); //10087