Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

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