Skip to content

Instantly share code, notes, and snippets.

@masakielastic
Created September 1, 2017 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masakielastic/9307344d9cee3d0c35829cd84cf2f60a to your computer and use it in GitHub Desktop.
Save masakielastic/9307344d9cee3d0c35829cd84cf2f60a to your computer and use it in GitHub Desktop.
utf8_ord
<?php
function utf8_ord($char) {
$x = ord($char[0]);
if ($x < 0x80) {
return $x;
} else if ($x < 0xE0) {
$y = ord($char[1]);
return (($x & 0b11111) << 6) + ($y & 0b111111);
} else if ($x < 0xF0) {
$y = ord($char[1]);
$z = ord($char[2]);
return (($x & 0b1111) << 12) + (($y & 0b111111) << 6) + ($z & 0b111111);
}
$y = ord($char[1]);
$z = ord($char[2]);
$w = ord($char[3]);
return (($x & 0b1111) << 18) + (($y & 0b111111) << 12) + (($z & 0b111111) << 6) + ($w & 0b111111);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment