Skip to content

Instantly share code, notes, and snippets.

@s3b4stian
Last active December 5, 2017 19:17
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 s3b4stian/d4863e9300bfeed448388a39d3af1d46 to your computer and use it in GitHub Desktop.
Save s3b4stian/d4863e9300bfeed448388a39d3af1d46 to your computer and use it in GitHub Desktop.
Return numerical part of the HTML encoding of the Unicode character.
<?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