source: trunk/Packages/Common/UTF8.php

Last change on this file was 970, checked in by chronos, 5 months ago
  • Modified: Service internet speed min and max used as contracted speeds. Real speed will be calculated with bonus speed and further queues calculations.
  • Added: Bonus internet speed service field.
  • Modified: Few PHP 8.3 fixes.
File size: 11.5 KB
Line 
1<?php
2/*
3 ISO8859-2 <-> UTF-8 conversion functions designed for use in PHP-GTK apps
4 Adam Rambousek - rambousek@volny.cz
5
6 version history:
7 1.03 --- 12/02/2002
8 * added Win1257 support
9 1.02 --- 30/11/2001
10 * added ISO8859-1 support
11 1.01
12 * added Win1250 support
13 1.00
14
15 string to_utf8(string string [, string charset])
16 string from_utf8(string string [, string charset])
17
18 supported charsets: name of charset you must use in script
19 ISO8859-2: iso2 (this is the default charset, you don't have to specify it)
20 Windows1250: win1250
21 ISO8859-1: iso1
22 Windows1257: win1257
23
24 example: $new_string=to_utf8($some_string,"win1250");
25*/
26
27
28/*
29 translation table - actually, it's array where key is hexadecimal number of
30 character in ISO8859-2/Windows1250 and value is its two byte representation in UTF-8
31*/
32
33class Encoding
34{
35 public array $CharTable;
36
37 function __construct()
38 {
39 $this->CharTable = array(
40 'iso2' => array(
41 0x80=>"\xc2\x80",
42 0x81=>"\xc2\x81",
43 0x82=>"\xc2\x82",
44 0x83=>"\xc2\x83",
45 0x84=>"\xc2\x84",
46 0x85=>"\xc2\x85",
47 0x86=>"\xc2\x86",
48 0x87=>"\xc2\x87",
49 0x88=>"\xc2\x88",
50 0x89=>"\xc2\x89",
51 0x8A=>"\xc2\x8a",
52 0x8B=>"\xc2\x8b",
53 0x8C=>"\xc2\x8c",
54 0x8D=>"\xc2\x8d",
55 0x8E=>"\xc2\x8e",
56 0x8F=>"\xc2\x8f",
57 0x90=>"\xc2\x90",
58 0x91=>"\xc2\x91",
59 0x92=>"\xc2\x92",
60 0x93=>"\xc2\x93",
61 0x94=>"\xc2\x94",
62 0x95=>"\xc2\x95",
63 0x96=>"\xc2\x96",
64 0x97=>"\xc2\x97",
65 0x98=>"\xc2\x98",
66 0x99=>"\xc2\x99",
67 0x9A=>"\xc2\x9a",
68 0x9B=>"\xc2\x9b",
69 0x9C=>"\xc2\x9c",
70 0x9D=>"\xc2\x9d",
71 0x9E=>"\xc2\x9e",
72 0x9F=>"\xc2\x9f",
73 0xA0=>"\xc2\xa0",
74 0xA1=>"\xc4\x84",
75 0xA2=>"\xcb\x98",
76 0xA3=>"\xc5\x81",
77 0xA4=>"\xc2\xa4",
78 0xA5=>"\xc4\xbd",
79 0xA6=>"\xc5\x9a",
80 0xA7=>"\xc2\xa7",
81 0xA8=>"\xc2\xa8",
82 0xA9=>"\xc5\xa0",
83 0xAA=>"\xc5\x9e",
84 0xAB=>"\xc5\xa4",
85 0xAC=>"\xc5\xb9",
86 0xAD=>"\xc2\xad",
87 0xAE=>"\xc5\xbd",
88 0xAF=>"\xc5\xbb",
89 0xB0=>"\xc2\xb0",
90 0xB1=>"\xc4\x85",
91 0xB2=>"\xcb\x9b",
92 0xB3=>"\xc5\x82",
93 0xB4=>"\xc2\xb4",
94 0xB5=>"\xc4\xbe",
95 0xB6=>"\xc5\x9b",
96 0xB7=>"\xcb\x87",
97 0xB8=>"\xc2\xb8",
98 0xB9=>"\xc5\xa1",
99 0xBA=>"\xc5\x9f",
100 0xBB=>"\xc5\xa5",
101 0xBC=>"\xc5\xba",
102 0xBD=>"\xcb\x9d",
103 0xBE=>"\xc5\xbe",
104 0xBF=>"\xc5\xbc",
105 0xC0=>"\xc5\x94",
106 0xC1=>"\xc3\x81",
107 0xC2=>"\xc3\x82",
108 0xC3=>"\xc4\x82",
109 0xC4=>"\xc3\x84",
110 0xC5=>"\xc4\xb9",
111 0xC6=>"\xc4\x86",
112 0xC7=>"\xc3\x87",
113 0xC8=>"\xc4\x8c",
114 0xC9=>"\xc3\x89",
115 0xCA=>"\xc4\x98",
116 0xCB=>"\xc3\x8b",
117 0xCC=>"\xc4\x9a",
118 0xCD=>"\xc3\x8d",
119 0xCE=>"\xc3\x8e",
120 0xCF=>"\xc4\x8e",
121 0xD0=>"\xc4\x90",
122 0xD1=>"\xc5\x83",
123 0xD2=>"\xc5\x87",
124 0xD3=>"\xc3\x93",
125 0xD4=>"\xc3\x94",
126 0xD5=>"\xc5\x90",
127 0xD6=>"\xc3\x96",
128 0xD7=>"\xc3\x97",
129 0xD8=>"\xc5\x98",
130 0xD9=>"\xc5\xae",
131 0xDA=>"\xc3\x9a",
132 0xDB=>"\xc5\xb0",
133 0xDC=>"\xc3\x9c",
134 0xDD=>"\xc3\x9d",
135 0xDE=>"\xc5\xa2",
136 0xDF=>"\xc3\x9f",
137 0xE0=>"\xc5\x95",
138 0xE1=>"\xc3\xa1",
139 0xE2=>"\xc3\xa2",
140 0xE3=>"\xc4\x83",
141 0xE4=>"\xc3\xa4",
142 0xE5=>"\xc4\xba",
143 0xE6=>"\xc4\x87",
144 0xE7=>"\xc3\xa7",
145 0xE8=>"\xc4\x8d",
146 0xE9=>"\xc3\xa9",
147 0xEA=>"\xc4\x99",
148 0xEB=>"\xc3\xab",
149 0xEC=>"\xc4\x9b",
150 0xED=>"\xc3\xad",
151 0xEE=>"\xc3\xae",
152 0xEF=>"\xc4\x8f",
153 0xF0=>"\xc4\x91",
154 0xF1=>"\xc5\x84",
155 0xF2=>"\xc5\x88",
156 0xF3=>"\xc3\xb3",
157 0xF4=>"\xc3\xb4",
158 0xF5=>"\xc5\x91",
159 0xF6=>"\xc3\xb6",
160 0xF7=>"\xc3\xb7",
161 0xF8=>"\xc5\x99",
162 0xF9=>"\xc5\xaf",
163 0xFA=>"\xc3\xba",
164 0xFB=>"\xc5\xb1",
165 0xFC=>"\xc3\xbc",
166 0xFD=>"\xc3\xbd",
167 0xFE=>"\xc5\xa3",
168 0xFF=>"\xcb\x99"
169 ),
170 'win1250' => array(
171 0x80=>"\xc2\x80",
172 0x81=>"\xc2\x81",
173 0x82=>"\xe2\x80\x9a",
174 0x83=>"\xc2\x83",
175 0x84=>"\xe2\x80\x9e",
176 0x85=>"\xe2\x80\xa6",
177 0x86=>"\xe2\x80\xa0",
178 0x87=>"\xe2\x80\xa1",
179 0x88=>"\xc2\x88",
180 0x89=>"\xe2\x80\xb0",
181 0x8a=>"\xc5\xa0",
182 0x8b=>"\xe2\x80\xb9",
183 0x8c=>"\xc5\x9a",
184 0x8d=>"\xc5\xa4",
185 0x8e=>"\xc5\xbd",
186 0x8f=>"\xc5\xb9",
187 0x90=>"\xc2\x90",
188 0x91=>"\xe2\x80\x98",
189 0x92=>"\xe2\x80\x99",
190 0x93=>"\xe2\x80\x9c",
191 0x94=>"\xe2\x80\x9d",
192 0x95=>"\xe2\x80\xa2",
193 0x96=>"\xe2\x80\x93",
194 0x97=>"\xe2\x80\x94",
195 0x98=>"\xe2\x80\x98",
196 0x99=>"\xe2\x84\xa2",
197 0x9a=>"\xc5\xa1",
198 0x9b=>"\xe2\x80\xba",
199 0x9c=>"\xc5\x9b",
200 0x9d=>"\xc5\xa5",
201 0x9e=>"\xc5\xbe",
202 0x9f=>"\xc5\xba",
203 0xa0=>"\xc2\xa0",
204 0xa1=>"\xcb\x87",
205 0xa2=>"\xcb\x98",
206 0xa3=>"\xc5\x81",
207 0xa4=>"\xc2\xa4",
208 0xa5=>"\xc4\x84",
209 0xa6=>"\xc2\xa6",
210 0xa7=>"\xc2\xa7",
211 0xa8=>"\xc2\xa8",
212 0xa9=>"\xc2\xa9",
213 0xaa=>"\xc5\x9e",
214 0xab=>"\xc2\xab",
215 0xac=>"\xc2\xac",
216 0xad=>"\xc2\xad",
217 0xae=>"\xc2\xae",
218 0xaf=>"\xc5\xbb",
219 0xb0=>"\xc2\xb0",
220 0xb1=>"\xc2\xb1",
221 0xb2=>"\xcb\x9b",
222 0xb3=>"\xc5\x82",
223 0xb4=>"\xc2\xb4",
224 0xb5=>"\xc2\xb5",
225 0xb6=>"\xc2\xb6",
226 0xb7=>"\xc2\xb7",
227 0xb8=>"\xc2\xb8",
228 0xb9=>"\xc4\x85",
229 0xba=>"\xc5\x9f",
230 0xbb=>"\xc2\xbb",
231 0xbc=>"\xc4\xbd",
232 0xbd=>"\xcb\x9d",
233 0xbe=>"\xc4\xbe",
234 0xbf=>"\xc5\xbc",
235 0xc0=>"\xc5\x94",
236 0xc1=>"\xc3\x81",
237 0xc2=>"\xc3\x82",
238 0xc3=>"\xc4\x82",
239 0xc4=>"\xc3\x84",
240 0xc5=>"\xc4\xb9",
241 0xc6=>"\xc4\x86",
242 0xc7=>"\xc3\x87",
243 0xc8=>"\xc4\x8c",
244 0xc9=>"\xc3\x89",
245 0xca=>"\xc4\x98",
246 0xcb=>"\xc3\x8b",
247 0xcc=>"\xc4\x9a",
248 0xcd=>"\xc3\x8d",
249 0xce=>"\xc3\x8e",
250 0xcf=>"\xc4\x8e",
251 0xd0=>"\xc4\x90",
252 0xd1=>"\xc5\x83",
253 0xd2=>"\xc5\x87",
254 0xd3=>"\xc3\x93",
255 0xd4=>"\xc3\x94",
256 0xd5=>"\xc5\x90",
257 0xd6=>"\xc3\x96",
258 0xd7=>"\xc3\x97",
259 0xd8=>"\xc5\x98",
260 0xd9=>"\xc5\xae",
261 0xda=>"\xc3\x9a",
262 0xdb=>"\xc5\xb0",
263 0xdc=>"\xc3\x9c",
264 0xdd=>"\xc3\x9d",
265 0xde=>"\xc5\xa2",
266 0xdf=>"\xc3\x9f",
267 0xe0=>"\xc5\x95",
268 0xe1=>"\xc3\xa1",
269 0xe2=>"\xc3\xa2",
270 0xe3=>"\xc4\x83",
271 0xe4=>"\xc3\xa4",
272 0xe5=>"\xc4\xba",
273 0xe6=>"\xc4\x87",
274 0xe7=>"\xc3\xa7",
275 0xe8=>"\xc4\x8d",
276 0xe9=>"\xc3\xa9",
277 0xea=>"\xc4\x99",
278 0xeb=>"\xc3\xab",
279 0xec=>"\xc4\x9b",
280 0xed=>"\xc3\xad",
281 0xee=>"\xc3\xae",
282 0xef=>"\xc4\x8f",
283 0xf0=>"\xc4\x91",
284 0xf1=>"\xc5\x84",
285 0xf2=>"\xc5\x88",
286 0xf3=>"\xc3\xb3",
287 0xf4=>"\xc3\xb4",
288 0xf5=>"\xc5\x91",
289 0xf6=>"\xc3\xb6",
290 0xf7=>"\xc3\xb7",
291 0xf8=>"\xc5\x99",
292 0xf9=>"\xc5\xaf",
293 0xfa=>"\xc3\xba",
294 0xfb=>"\xc5\xb1",
295 0xfc=>"\xc3\xbc",
296 0xfd=>"\xc3\xbd",
297 0xfe=>"\xc5\xa3",
298 0xff=>"\xcb\x99"
299 ),
300 'iso1' => array(
301 0xA0=>"\xc2\xa0",
302 0xA1=>"\xc2\xa1",
303 0xA2=>"\xc2\xa2",
304 0xA3=>"\xc2\xa3",
305 0xA4=>"\xc2\xa4",
306 0xA5=>"\xc2\xa5",
307 0xA6=>"\xc2\xa6",
308 0xA7=>"\xc2\xa7",
309 0xA8=>"\xc2\xa8",
310 0xA9=>"\xc2\xa9",
311 0xAA=>"\xc2\xaa",
312 0xAB=>"\xc2\xab",
313 0xAC=>"\xc2\xac",
314 0xAD=>"\xc2\xad",
315 0xAE=>"\xc2\xae",
316 0xAF=>"\xc2\xaf",
317 0xB0=>"\xc2\xb0",
318 0xB1=>"\xc2\xb1",
319 0xB2=>"\xc2\xb2",
320 0xB3=>"\xc2\xb3",
321 0xB4=>"\xc2\xb4",
322 0xB5=>"\xc2\xb5",
323 0xB6=>"\xc2\xb6",
324 0xB7=>"\xc2\xb7",
325 0xB8=>"\xc2\xb8",
326 0xB9=>"\xc2\xb9",
327 0xBA=>"\xc2\xba",
328 0xBB=>"\xc2\xbb",
329 0xBC=>"\xc2\xbc",
330 0xBD=>"\xc2\xbd",
331 0xBE=>"\xc2\xbe",
332 0xBF=>"\xc2\xbf",
333 0xC0=>"\xc3\x80",
334 0xC1=>"\xc3\x81",
335 0xC2=>"\xc3\x82",
336 0xC3=>"\xc3\x83",
337 0xC4=>"\xc3\x84",
338 0xC5=>"\xc3\x85",
339 0xC6=>"\xc3\x86",
340 0xC7=>"\xc3\x87",
341 0xC8=>"\xc3\x88",
342 0xC9=>"\xc3\x89",
343 0xCA=>"\xc3\x8a",
344 0xCB=>"\xc3\x8b",
345 0xCC=>"\xc3\x8c",
346 0xCD=>"\xc3\x8d",
347 0xCE=>"\xc3\x8e",
348 0xCF=>"\xc3\x8f",
349 0xD0=>"\xc3\x90",
350 0xD1=>"\xc3\x91",
351 0xD2=>"\xc3\x92",
352 0xD3=>"\xc3\x93",
353 0xD4=>"\xc3\x94",
354 0xD5=>"\xc3\x95",
355 0xD6=>"\xc3\x96",
356 0xD7=>"\xc3\x97",
357 0xD8=>"\xc3\x98",
358 0xD9=>"\xc3\x99",
359 0xDA=>"\xc3\x9a",
360 0xDB=>"\xc3\x9b",
361 0xDC=>"\xc3\x9c",
362 0xDD=>"\xc3\x9d",
363 0xDE=>"\xc3\x9e",
364 0xDF=>"\xc3\x9f",
365 0xE0=>"\xc3\xa0",
366 0xE1=>"\xc3\xa1",
367 0xE2=>"\xc3\xa2",
368 0xE3=>"\xc3\xa3",
369 0xE4=>"\xc3\xa4",
370 0xE5=>"\xc3\xa5",
371 0xE6=>"\xc3\xa6",
372 0xE7=>"\xc3\xa7",
373 0xE8=>"\xc3\xa8",
374 0xE9=>"\xc3\xa9",
375 0xEA=>"\xc3\xaa",
376 0xEB=>"\xc3\xab",
377 0xEC=>"\xc3\xac",
378 0xED=>"\xc3\xad",
379 0xEE=>"\xc3\xae",
380 0xEF=>"\xc3\xaf",
381 0xF0=>"\xc3\xb0",
382 0xF1=>"\xc3\xb1",
383 0xF2=>"\xc3\xb2",
384 0xF3=>"\xc3\xb3",
385 0xF4=>"\xc3\xb4",
386 0xF5=>"\xc3\xb5",
387 0xF6=>"\xc3\xb6",
388 0xF7=>"\xc3\xb7",
389 0xF8=>"\xc3\xb8",
390 0xF9=>"\xc3\xb9",
391 0xFA=>"\xc3\xba",
392 0xFB=>"\xc3\xbb",
393 0xFC=>"\xc3\xbc",
394 0xFD=>"\xc3\xbd",
395 0xFE=>"\xc3\xbe"
396 ),
397 'win1257' => array(
398 0x80=>"\xe2\x82\xac",
399 0x81=>"\xc0\x4",
400 0x82=>"\xe2\x80\x9a",
401 0x83=>"\xc0\x4",
402 0x84=>"\xe2\x80\x9e",
403 0x85=>"\xe2\x80\xa6",
404 0x86=>"\xe2\x80\xa0",
405 0x87=>"\xe2\x80\xa1",
406 0x88=>"\xc0\x4",
407 0x89=>"\xe2\x80\xb0",
408 0x8A=>"\xc0\x4",
409 0x8B=>"\xe2\x80\xb9",
410 0x8C=>"\xc0\x4",
411 0x8D=>"\xc2\xa8",
412 0x8E=>"\xcb\x87",
413 0x8F=>"\xc2\xb8",
414 0x90=>"\xc0\x4",
415 0x91=>"\xe2\x80\x98",
416 0x92=>"\xe2\x80\x99",
417 0x93=>"\xe2\x80\x9c",
418 0x94=>"\xe2\x80\x9d",
419 0x95=>"\xe2\x80\xa2",
420 0x96=>"\xe2\x80\x93",
421 0x97=>"\xe2\x80\x94",
422 0x98=>"\xc0\x4",
423 0x99=>"\xe2\x84\xa2",
424 0x9A=>"\xc0\x4",
425 0x9B=>"\xe2\x80\xba",
426 0x9C=>"\xc0\x4",
427 0x9D=>"\xc2\xaf",
428 0x9E=>"\xcb\x9b",
429 0x9F=>"\xc0\x4",
430 0xA0=>"\xc2\xa0",
431 0xA1=>"\xc0\x4",
432 0xA2=>"\xc2\xa2",
433 0xA3=>"\xc2\xa3",
434 0xA4=>"\xc2\xa4",
435 0xA5=>"\xc0\x4",
436 0xA6=>"\xc2\xa6",
437 0xA7=>"\xc2\xa7",
438 0xA8=>"\xc3\x98",
439 0xA9=>"\xc2\xa9",
440 0xAA=>"\xc5\x96",
441 0xAB=>"\xc2\xab",
442 0xAC=>"\xc2\xac",
443 0xAD=>"\xc2\xad",
444 0xAE=>"\xc2\xae",
445 0xAF=>"\xc3\x86",
446 0xB0=>"\xc2\xb0",
447 0xB1=>"\xc2\xb1",
448 0xB2=>"\xc2\xb2",
449 0xB3=>"\xc2\xb3",
450 0xB4=>"\xc2\xb4",
451 0xB5=>"\xc2\xb5",
452 0xB6=>"\xc2\xb6",
453 0xB7=>"\xc2\xb7",
454 0xB8=>"\xc3\xb8",
455 0xB9=>"\xc2\xb9",
456 0xBA=>"\xc5\x97",
457 0xBB=>"\xc2\xbb",
458 0xBC=>"\xc2\xbc",
459 0xBD=>"\xc2\xbd",
460 0xBE=>"\xc2\xbe",
461 0xBF=>"\xc3\xa6",
462 0xC0=>"\xc4\x84",
463 0xC1=>"\xc4\xae",
464 0xC2=>"\xc4\x80",
465 0xC3=>"\xc4\x86",
466 0xC4=>"\xc3\x84",
467 0xC5=>"\xc3\x85",
468 0xC6=>"\xc4\x98",
469 0xC7=>"\xc4\x92",
470 0xC8=>"\xc4\x8c",
471 0xC9=>"\xc3\x89",
472 0xCA=>"\xc5\xb9",
473 0xCB=>"\xc4\x96",
474 0xCC=>"\xc4\xa2",
475 0xCD=>"\xc4\xb6",
476 0xCE=>"\xc4\xaa",
477 0xCF=>"\xc4\xbb",
478 0xD0=>"\xc5\xa0",
479 0xD1=>"\xc5\x83",
480 0xD2=>"\xc5\x85",
481 0xD3=>"\xc3\x93",
482 0xD4=>"\xc5\x8c",
483 0xD5=>"\xc3\x95",
484 0xD6=>"\xc3\x96",
485 0xD7=>"\xc3\x97",
486 0xD8=>"\xc5\xb2",
487 0xD9=>"\xc5\x81",
488 0xDA=>"\xc5\x9a",
489 0xDB=>"\xc5\xaa",
490 0xDC=>"\xc3\x9c",
491 0xDD=>"\xc5\xbb",
492 0xDE=>"\xc5\xbd",
493 0xDF=>"\xc3\x9f",
494 0xE0=>"\xc4\x85",
495 0xE1=>"\xc4\xaf",
496 0xE2=>"\xc4\x81",
497 0xE3=>"\xc4\x87",
498 0xE4=>"\xc3\xa4",
499 0xE5=>"\xc3\xa5",
500 0xE6=>"\xc4\x99",
501 0xE7=>"\xc4\x93",
502 0xE8=>"\xc4\x8d",
503 0xE9=>"\xc3\xa9",
504 0xEA=>"\xc5\xba",
505 0xEB=>"\xc4\x97",
506 0xEC=>"\xc4\xa3",
507 0xED=>"\xc4\xb7",
508 0xEE=>"\xc4\xab",
509 0xEF=>"\xc4\xbc",
510 0xF0=>"\xc5\xa1",
511 0xF1=>"\xc5\x84",
512 0xF2=>"\xc5\x86",
513 0xF3=>"\xc3\xb3",
514 0xF4=>"\xc5\x8d",
515 0xF5=>"\xc3\xb5",
516 0xF6=>"\xc3\xb6",
517 0xF7=>"\xc3\xb7",
518 0xF8=>"\xc5\xb3",
519 0xF9=>"\xc5\x82",
520 0xFA=>"\xc5\x9b",
521 0xFB=>"\xc5\xab",
522 0xFC=>"\xc3\xbc",
523 0xFD=>"\xc5\xbc",
524 0xFE=>"\xc5\xbe",
525 0xFF=>"\xcb\x99"
526 ),
527);
528 }
529
530 function ToUTF8(string $String, string $Charset = 'iso2'): string
531 {
532 $Result = '';
533 for ($I = 0; $I < strlen($String); $I++)
534 {
535 if (ord($String[$I]) < 128) $Result .= $String[$I];
536 else if (ord($String[$I]) > 127)
537 {
538 $Result .= $this->CharTable[$Charset][ord($String[$I])];
539 }
540 }
541 return $Result;
542 }
543
544 function FromUTF8(string $String, string $Charset = 'iso2'): string
545 {
546 $Result = '';
547 $UTFPrefix = '';
548 for ($I = 0; $I < strlen($String); $I++)
549 {
550 if (ord($String[$I]) & 0x80) // UTF control character
551 {
552 if (ord($String[$I]) & 0x40) // First
553 {
554 if ($UTFPrefix != '') $Result .= chr(array_search($UTFPrefix, $this->CharTable[$Charset]));
555 $UTFPrefix = $String[$I];
556 }
557 else $UTFPrefix .= $String[$I]; // Next
558 } else
559 {
560 if ($UTFPrefix != '') $Result .= chr(array_search($UTFPrefix, $this->CharTable[$Charset]));
561 $UTFPrefix = '';
562 $Result .= $String[$I];
563 }
564 }
565 return $Result;
566 }
567}
Note: See TracBrowser for help on using the repository browser.