Last active
December 5, 2017 19:17
-
-
Save s3b4stian/d4863e9300bfeed448388a39d3af1d46 to your computer and use it in GitHub Desktop.
Return numerical part of the HTML encoding of the Unicode character.
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 | |
/** | |
* Return numerical part of the HTML encoding of the Unicode character. | |
* | |
* @param string $char | |
* @return int | |
*/ | |
function ordutf8(string $char) : int | |
{ | |
$code = ord(substr($char, 0, 1)); | |
if ($code > 127) { | |
//110xxxxx | |
$bytes = 2; | |
$count = 0; | |
if ($code > 223) { | |
//1110xxxx | |
$bytes = 3; | |
$count = 32; | |
} | |
if ($code > 239) { | |
//11110xxx | |
$bytes = 4; | |
$count = 48; | |
} | |
$temp = $code - 192 - $count; | |
for ($i = 1; $i < $bytes; $i++) { | |
$code = $temp = $temp * 64 + ord(substr($char, $i, 1)) - 128; | |
} | |
} | |
return $code; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment