Last active
August 29, 2015 14:17
-
-
Save cameronjacobson/88cf0c0de06390ddedd6 to your computer and use it in GitHub Desktop.
utf8 decode a unicode code point
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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