Skip to content

Instantly share code, notes, and snippets.

@Ostico
Last active September 14, 2016 17:20
Show Gist options
  • Save Ostico/4b8bede4b5c11ddc240c98a773d1551a to your computer and use it in GitHub Desktop.
Save Ostico/4b8bede4b5c11ddc240c98a773d1551a to your computer and use it in GitHub Desktop.
<?php
/**
* Get the char code from a multi byte char
*
* 2/3 times faster than the old implementation
*
* @param $mb_char string Unicode Multibyte Char String
*
* @return int
*
*/
function fastUnicode2ord( $mb_char ){
switch( strlen( $mb_char) ){
case 1:
return ord( $mb_char);
break;
case 2:
return ( ord( $mb_char[0] ) - 0xC0 ) * 0x40 +
ord( $mb_char[1] ) - 0x80;
break;
case 3:
return ( ord( $mb_char[0] ) - 0xE0 ) * 0x1000 +
( ord( $mb_char[1] ) - 0x80 ) * 0x40 +
ord( $mb_char[2] ) - 0x80;
break;
case 4:
return ( ord( $mb_char[0] ) - 0xF0 ) * 0x40000 +
( ord( $mb_char[1] ) - 0x80 ) * 0x1000 +
( ord( $mb_char[2] ) - 0x80 ) * 0x40 +
ord( $mb_char[3] ) - 0x80;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment