3 * @version $Id: ord.php,v 1.4 2006/09/11 15:22:54 harryf Exp $
8 //---------------------------------------------------------------
10 * UTF-8 aware alternative to ord
11 * Returns the unicode ordinal for a character
12 * @param string UTF-8 encoded character
13 * @return int unicode ordinal for the character
14 * @see http://www.php.net/ord
15 * @see http://www.php.net/manual/en/function.ord.php#46267
17 function utf8_ord($chr) {
21 if ( $ord0 >= 0 && $ord0 <= 127 ) {
25 if ( !isset($chr{1}) ) {
26 trigger_error('Short sequence - at least 2 bytes expected, only 1 seen');
31 if ( $ord0 >= 192 && $ord0 <= 223 ) {
32 return ( $ord0 - 192 ) * 64
36 if ( !isset($chr{2}) ) {
37 trigger_error('Short sequence - at least 3 bytes expected, only 2 seen');
41 if ( $ord0 >= 224 && $ord0 <= 239 ) {
42 return ($ord0-224)*4096
47 if ( !isset($chr{3}) ) {
48 trigger_error('Short sequence - at least 4 bytes expected, only 3 seen');
52 if ($ord0>=240 && $ord0<=247) {
53 return ($ord0-240)*262144
60 if ( !isset($chr{4}) ) {
61 trigger_error('Short sequence - at least 5 bytes expected, only 4 seen');
65 if ($ord0>=248 && $ord0<=251) {
66 return ($ord0-248)*16777216
73 if ( !isset($chr{5}) ) {
74 trigger_error('Short sequence - at least 6 bytes expected, only 5 seen');
77 if ($ord0>=252 && $ord0<=253) {
78 return ($ord0-252) * 1073741824
79 + ($ord1-128)*16777216
86 if ( $ord0 >= 254 && $ord0 <= 255 ) {
87 trigger_error('Invalid UTF-8 with surrogate ordinal '.$ord0);