1 <?php
2 3 4
5
6
7 8 9 10 11 12 13 14
15 function utf8_ord($chr) {
16
17 $ord0 = ord($chr);
18
19 if ( $ord0 >= 0 && $ord0 <= 127 ) {
20 return $ord0;
21 }
22
23 if ( !isset($chr{1}) ) {
24 trigger_error('Short sequence - at least 2 bytes expected, only 1 seen');
25 return FALSE;
26 }
27
28 $ord1 = ord($chr{1});
29 if ( $ord0 >= 192 && $ord0 <= 223 ) {
30 return ( $ord0 - 192 ) * 64
31 + ( $ord1 - 128 );
32 }
33
34 if ( !isset($chr{2}) ) {
35 trigger_error('Short sequence - at least 3 bytes expected, only 2 seen');
36 return FALSE;
37 }
38 $ord2 = ord($chr{2});
39 if ( $ord0 >= 224 && $ord0 <= 239 ) {
40 return ($ord0-224)*4096
41 + ($ord1-128)*64
42 + ($ord2-128);
43 }
44
45 if ( !isset($chr{3}) ) {
46 trigger_error('Short sequence - at least 4 bytes expected, only 3 seen');
47 return FALSE;
48 }
49 $ord3 = ord($chr{3});
50 if ($ord0>=240 && $ord0<=247) {
51 return ($ord0-240)*262144
52 + ($ord1-128)*4096
53 + ($ord2-128)*64
54 + ($ord3-128);
55
56 }
57
58 if ( !isset($chr{4}) ) {
59 trigger_error('Short sequence - at least 5 bytes expected, only 4 seen');
60 return FALSE;
61 }
62 $ord4 = ord($chr{4});
63 if ($ord0>=248 && $ord0<=251) {
64 return ($ord0-248)*16777216
65 + ($ord1-128)*262144
66 + ($ord2-128)*4096
67 + ($ord3-128)*64
68 + ($ord4-128);
69 }
70
71 if ( !isset($chr{5}) ) {
72 trigger_error('Short sequence - at least 6 bytes expected, only 5 seen');
73 return FALSE;
74 }
75 if ($ord0>=252 && $ord0<=253) {
76 return ($ord0-252) * 1073741824
77 + ($ord1-128)*16777216
78 + ($ord2-128)*262144
79 + ($ord3-128)*4096
80 + ($ord4-128)*64
81 + (ord($chr{5})-128);
82 }
83
84 if ( $ord0 >= 254 && $ord0 <= 255 ) {
85 trigger_error('Invalid UTF-8 with surrogate ordinal '.$ord0);
86 return FALSE;
87 }
88
89 }
90
91