Skip to content

Instantly share code, notes, and snippets.

@cameronjacobson
Last active August 29, 2015 14:17
Show Gist options
  • Save cameronjacobson/88cf0c0de06390ddedd6 to your computer and use it in GitHub Desktop.
Save cameronjacobson/88cf0c0de06390ddedd6 to your computer and use it in GitHub Desktop.
utf8 decode a unicode code point
<?php
// convert utf8 character to its unicode code point
function utf8decode($chr){
switch(strlen($chr)){
case 1:
$x = unpack('c',$chr);
$dec = $x[1] & 127;
break;
case 2:
$x = unpack('c*',$chr);
$dec = (($x[1] & 31) << 6)
| ($x[2] & 63);
break;
case 3:
$x = unpack('c*',$chr);
$dec = (($x[1] & 15) << 12)
| (($x[2] & 63) << 6)
| ($x[3] & 63);
break;
case 4:
$x = unpack('c*',$chr);
$dec = (($x[1] & 7) << 18)
| (($x[2] & 63) << 12)
| (($x[3] & 63) << 6)
| ($x[4] & 63);
break;
}
return dechex($dec);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment