Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezutf8codec.php
Go to the documentation of this file.
1 <?php
21 class eZUTF8Codec
22 {
26 static function convertStringToUnicode( $str )
27 {
28 $unicodeValues = array();
29 $strLen = strlen( $str );
30 for ( $offset = 0; $offset < $strLen; )
31 {
32 $charLen = 1;
33 $unicodeValue = eZUTF8Codec::fromUTF8( $str, $offset, $charLen );
34 if ( $unicodeValue !== false )
35 $unicodeValues[] = $unicodeValue;
36 $offset += $charLen;
37 }
38 return $unicodeValues;
39 }
40
44 static function convertUnicodeToString( $unicodeValues )
45 {
46 if ( !is_array( $unicodeValues ) )
47 return false;
48 $text = '';
49 foreach ( $unicodeValues as $unicodeValue )
50 {
51 $utf8Char = eZUTF8Codec::toUTF8( $unicodeValue );
52 $text .= $utf8Char;
53 }
54 return $text;
55 }
56
61 static function toUTF8( $char_code )
62 {
63 switch ( $char_code )
64 {
65 case 0:
66 $char = chr( 0 );
67 case !($char_code & 0xffffff80): // 7 bit
68 $char = chr( $char_code );
69 break;
70 case !($char_code & 0xfffff800): // 11 bit
71 $char = ( chr(0xc0 | (($char_code >> 6) & 0x1f)) .
72 chr(0x80 | ($char_code & 0x3f)) );
73 break;
74 case !($char_code & 0xffff0000): // 16 bit
75 $char = ( chr(0xe0 | (($char_code >> 12) & 0x0f)) .
76 chr(0x80 | (($char_code >> 6) & 0x3f)) .
77 chr(0x80 | ($char_code & 0x3f)) );
78 break;
79 case !($char_code & 0xffe00000): // 21 bit
80 $char = ( chr(0xf0 | (($char_code >> 18) & 0x07)) .
81 chr(0x80 | (($char_code >> 12) & 0x3f)) .
82 chr(0x80 | (($char_code >> 6) & 0x3f)) .
83 chr(0x80 | ($char_code & 0x3f)) );
84 break;
85 case !($char_code & 0xfc000000): // 26 bit
86 $char = ( chr(0xf8 | (($char_code >> 24) & 0x03)) .
87 chr(0x80 | (($char_code >> 18) & 0x3f)) .
88 chr(0x80 | (($char_code >> 12) & 0x3f)) .
89 chr(0x80 | (($char_code >> 6) & 0x3f)) .
90 chr(0x80 | ($char_code & 0x3f)) );
91 default: // 31 bit
92 $char = ( chr(0xfc | (($char_code >> 30) & 0x01)) .
93 chr(0x80 | (($char_code >> 24) & 0x3f)) .
94 chr(0x80 | (($char_code >> 18) & 0x3f)) .
95 chr(0x80 | (($char_code >> 12) & 0x3f)) .
96 chr(0x80 | (($char_code >> 6) & 0x3f)) .
97 chr(0x80 | ($char_code & 0x3f)) );
98 }
99 return $char;
100 }
101
109 static function fromUtf8( $multi_char, $offs, &$len )
110 {
111 $char_code = false;
112 if ( ( ord( $multi_char[$offs + 0] ) & 0x80 ) == 0x00 ) // 7 bit, 1 char
113 {
114 $char_code = ord( $multi_char[$offs + 0] );
115 $len = 1;
116 }
117 else if ( ( ord( $multi_char[$offs + 0] ) & 0xe0 ) == 0xc0 ) // 11 bit, 2 chars
118 {
119 $len = 2;
120 if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 )
121 return $char_code;
122 $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x1f ) << 6) +
123 (( ord( $multi_char[$offs + 1] ) & 0x3f )) );
124 if ( $char_code < 128 ) // Illegal multibyte, should use less than 2 chars
125 {
126 $char_code = false;
127 }
128 }
129 else if ( ( ord( $multi_char[$offs + 0] ) & 0xf0 ) == 0xe0 ) // 16 bit, 3 chars
130 {
131 $len = 3;
132 if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 or
133 ( ord( $multi_char[$offs + 2] ) & 0xc0 ) != 0x80 )
134 return $char_code;
135 $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x0f ) << 12) +
136 (( ord( $multi_char[$offs + 1] ) & 0x3f ) << 6) +
137 (( ord( $multi_char[$offs + 2] ) & 0x3f )) );
138 if ( $char_code < 2048 ) // Illegal multibyte, should use less than 3 chars
139 {
140 $char_code = false;
141 }
142 }
143 else if ( ( ord( $multi_char[$offs + 0] ) & 0xf8 ) == 0xf0 ) // 21 bit, 4 chars
144 {
145 $len = 4;
146 if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 or
147 ( ord( $multi_char[$offs + 2] ) & 0xc0 ) != 0x80 or
148 ( ord( $multi_char[$offs + 3] ) & 0xc0 ) != 0x80 )
149 return $char_code;
150 $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x07 ) << 18) +
151 (( ord( $multi_char[$offs + 1] ) & 0x3f ) << 12) +
152 (( ord( $multi_char[$offs + 2] ) & 0x3f ) << 6) +
153 (( ord( $multi_char[$offs + 3] ) & 0x3f )) );
154 if ( $char_code < 65536 ) // Illegal multibyte, should use less than 4 chars
155 {
156 $char_code = false;
157 }
158 }
159 else if ( ( ord( $multi_char[$offs + 0] ) & 0xfc ) == 0xf8 ) // 26 bit, 5 chars
160 {
161 $len = 5;
162 if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 or
163 ( ord( $multi_char[$offs + 2] ) & 0xc0 ) != 0x80 or
164 ( ord( $multi_char[$offs + 3] ) & 0xc0 ) != 0x80 or
165 ( ord( $multi_char[$offs + 4] ) & 0xc0 ) != 0x80 )
166 return $char_code;
167 $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x03 ) << 24) +
168 (( ord( $multi_char[$offs + 1] ) & 0x3f ) << 18) +
169 (( ord( $multi_char[$offs + 2] ) & 0x3f ) << 12) +
170 (( ord( $multi_char[$offs + 3] ) & 0x3f ) << 6) +
171 (( ord( $multi_char[$offs + 4] ) & 0x3f )) );
172 if ( $char_code < 2097152 ) // Illegal multibyte, should use less than 5 chars
173 {
174 $char_code = false;
175 }
176 }
177 else if ( ( ord( $multi_char[$offs + 0] ) & 0xfe ) == 0xfc ) // 31 bit, 6 chars
178 {
179 $len = 6;
180 if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 or
181 ( ord( $multi_char[$offs + 2] ) & 0xc0 ) != 0x80 or
182 ( ord( $multi_char[$offs + 3] ) & 0xc0 ) != 0x80 or
183 ( ord( $multi_char[$offs + 4] ) & 0xc0 ) != 0x80 or
184 ( ord( $multi_char[$offs + 5] ) & 0xc0 ) != 0x80 )
185 return $char_code;
186 $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x01 ) << 30) +
187 (( ord( $multi_char[$offs + 1] ) & 0x3f ) << 24) +
188 (( ord( $multi_char[$offs + 2] ) & 0x3f ) << 18) +
189 (( ord( $multi_char[$offs + 3] ) & 0x3f ) << 12) +
190 (( ord( $multi_char[$offs + 4] ) & 0x3f ) << 6) +
191 (( ord( $multi_char[$offs + 5] ) & 0x3f )) );
192 if ( $char_code < 67108864 ) // Illegal multibyte, should use less than 6 chars
193 {
194 $char_code = false;
195 }
196 }
197 return $char_code;
198 }
199
200 static function utf8LengthTable()
201 {
202 if ( empty( $GLOBALS['eZUTF8LengthTable'] ) )
203 {
204 $GLOBALS['eZUTF8LengthTable'] =
205 array( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
206 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
207 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6 );
208 }
209 return $GLOBALS['eZUTF8LengthTable'];
210 }
211
212 static function characterByteLength( $str, $pos )
213 {
214 $table = eZUTF8Codec::utf8LengthTable();
215 $char = ord( $str[$pos] );
216 return $table[($char >> 2) & 0x3f];
217 }
218
219 static function strlen( $str )
220 {
221 $table = eZUTF8Codec::utf8LengthTable();
222 $len = strlen( $str );
223 $strlen = 0;
224 for ( $i = 0; $i < $len; )
225 {
226 $char = ord( $str[$i] );
227 $char_len = $table[($char >> 2) & 0x3f];
228 $i += $char_len;
229 ++$strlen;
230 }
231 return $strlen;
232 }
233
239 static function instance()
240 {
241 if ( empty( $GLOBALS['eZUTF8CodecInstance'] ) )
242 {
243 $GLOBALS['eZUTF8CodecInstance'] = new eZUTF8Codec();
244 }
245 return $GLOBALS['eZUTF8CodecInstance'];
246 }
247 }
248
249 ?>
eZUTF8Codec
Converter for utf8 and 32bit unicode.
Definition: ezutf8codec.php:22
eZUTF8Codec\convertStringToUnicode
static convertStringToUnicode( $str)
Definition: ezutf8codec.php:26
eZUTF8Codec\fromUtf8
static fromUtf8( $multi_char, $offs, &$len)
Definition: ezutf8codec.php:109
eZUTF8Codec\convertUnicodeToString
static convertUnicodeToString( $unicodeValues)
Definition: ezutf8codec.php:44
eZUTF8Codec\strlen
static strlen( $str)
Definition: ezutf8codec.php:219
eZUTF8Codec\toUTF8
static toUTF8( $char_code)
Definition: ezutf8codec.php:61
eZUTF8Codec\utf8LengthTable
static utf8LengthTable()
Definition: ezutf8codec.php:200
eZUTF8Codec\characterByteLength
static characterByteLength( $str, $pos)
Definition: ezutf8codec.php:212
eZUTF8Codec\instance
static instance()
Definition: ezutf8codec.php:239
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.