-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUnicode.php
1227 lines (998 loc) · 49.7 KB
/
Unicode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/******************************************************************************
*
* Filename: Unicode.php
*
* Description: Provides functions for handling Unicode strings in PHP without
* needing to configure the non-default mbstring extension
*
* Author: Evan Hunter
*
* Date: 27/7/2004
*
* Project: JPEG Metadata
*
* Revision: 1.10
*
* Changes: 1.00 -> 1.10 : Added the following functions:
* smart_HTML_Entities
* smart_htmlspecialchars
* HTML_UTF16_UnEscape
* HTML_UTF8_UnEscape
* changed HTML_UTF8_Escape and HTML_UTF16_Escape to
* use smart_htmlspecialchars, so that characters which
* were already escaped would remain intact
*
*
* URL: http://electronics.ozhiker.com
*
* License: This file is part of the PHP JPEG Metadata Toolkit.
*
* The PHP JPEG Metadata Toolkit is free software; you can
* redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* The PHP JPEG Metadata Toolkit is distributed in the hope
* that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public
* License along with the PHP JPEG Metadata Toolkit; if not,
* write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*
* If you require a different license for commercial or other
* purposes, please contact the author: [email protected]
*
******************************************************************************/
// TODO: UTF-16 functions have not been tested fully
/******************************************************************************
*
* Unicode UTF-8 Encoding Functions
*
* Description: UTF-8 is a Unicode encoding system in which extended characters
* use only the upper half (128 values) of the byte range, thus it
* allows the use of normal 7-bit ASCII text.
* 7-Bit ASCII will pass straight through UTF-8 encoding/decoding without change
*
*
* The encoding is as follows:
* Unicode Value : Binary representation (x=data bit)
*--------------------------------------------------------------------------------
* U-00000000 - U-0000007F: 0xxxxxxx <- This is 7-bit ASCII
* U-00000080 - U-000007FF: 110xxxxx 10xxxxxx
* U-00000800 - U-0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx
* U-00010000 - U-001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
* U-00200000 - U-03FFFFFF: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
* U-04000000 - U-7FFFFFFF: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
*--------------------------------------------------------------------------------
*
******************************************************************************/
/******************************************************************************
*
* Unicode UTF-16 Encoding Functions
*
* Description: UTF-16 is a Unicode encoding system uses 16 bit values for representing
* characters.
* It also has an extended set of characters available by the use
* of surrogate pairs, which are a pair of 16 bit values, giving a
* total data length of 20 useful bits.
*
*
* The encoding is as follows:
* Unicode Value : Binary representation (x=data bit)
*--------------------------------------------------------------------------------
* U-000000 - U-00D7FF: xxxxxxxx xxxxxxxx
* U-00D800 - U-00DBFF: Not available - used for high surrogate pairs
* U-00DC00 - U-00DFFF: Not available - used for low surrogate pairs
U-00E000 - U-00FFFF: xxxxxxxx xxxxxxxx
* U-010000 - U-10FFFF: 110110ww wwxxxxxx 110111xx xxxxxxxx ( wwww = (uni-0x10000)/0x10000 )
*--------------------------------------------------------------------------------
*
* Surrogate pair Calculations
*
* $hi = ($uni - 0x10000) / 0x400 + 0xD800;
* $lo = ($uni - 0x10000) % 0x400 + 0xDC00;
*
*
* $uni = 0x10000 + ($hi - 0xD800) * 0x400 + ($lo - 0xDC00);
*
*
******************************************************************************/
/******************************************************************************
*
* Function: UTF8_fix
*
* Description: Checks a string for badly formed Unicode UTF-8 coding and
* returns the same string containing only the parts which
* were properly formed UTF-8 data.
*
* Parameters: utf8_text - a string with possibly badly formed UTF-8 data
*
* Returns: output - the well formed UTF-8 version of the string
*
******************************************************************************/
function UTF8_fix( $utf8_text )
{
// Initialise the current position in the string
$pos = 0;
// Create a string to accept the well formed output
$output = "" ;
// Cycle through each group of bytes, ensuring the coding is correct
while ( $pos < strlen( $utf8_text ) )
{
// Retreive the current numerical character value
$chval = ord($utf8_text{$pos});
// Check what the first character is - it will tell us how many bytes the
// Unicode value covers
if ( ( $chval >= 0x00 ) && ( $chval <= 0x7F ) )
{
// 1 Byte UTF-8 Unicode (7-Bit ASCII) Character
$bytes = 1;
}
else if ( ( $chval >= 0xC0 ) && ( $chval <= 0xDF ) )
{
// 2 Byte UTF-8 Unicode Character
$bytes = 2;
}
else if ( ( $chval >= 0xE0 ) && ( $chval <= 0xEF ) )
{
// 3 Byte UTF-8 Unicode Character
$bytes = 3;
}
else if ( ( $chval >= 0xF0 ) && ( $chval <= 0xF7 ) )
{
// 4 Byte UTF-8 Unicode Character
$bytes = 4;
}
else if ( ( $chval >= 0xF8 ) && ( $chval <= 0xFB ) )
{
// 5 Byte UTF-8 Unicode Character
$bytes = 5;
}
else if ( ( $chval >= 0xFC ) && ( $chval <= 0xFD ) )
{
// 6 Byte UTF-8 Unicode Character
$bytes = 6;
}
else
{
// Invalid Code - skip character and do nothing
$bytes = 0;
$pos++;
}
// check that there is enough data remaining to read
if (($pos + $bytes - 1) < strlen( $utf8_text ) )
{
// Cycle through the number of bytes specified,
// copying them to the output string
while ( $bytes > 0 )
{
$output .= $utf8_text{$pos};
$pos++;
$bytes--;
}
}
else
{
break;
}
}
// Return the result
return $output;
}
/******************************************************************************
* End of Function: UTF8_fix
******************************************************************************/
/******************************************************************************
*
* Function: UTF16_fix
*
* Description: Checks a string for badly formed Unicode UTF-16 coding and
* returns the same string containing only the parts which
* were properly formed UTF-16 data.
*
* Parameters: utf16_text - a string with possibly badly formed UTF-16 data
* MSB_first - True will cause processing as Big Endian UTF-16 (Motorola, MSB first)
* False will cause processing as Little Endian UTF-16 (Intel, LSB first)
*
* Returns: output - the well formed UTF-16 version of the string
*
******************************************************************************/
function UTF16_fix( $utf16_text, $MSB_first )
{
// Initialise the current position in the string
$pos = 0;
// Create a string to accept the well formed output
$output = "" ;
// Cycle through each group of bytes, ensuring the coding is correct
while ( $pos < strlen( $utf16_text ) )
{
// Retreive the current numerical character value
$chval1 = ord($utf16_text{$pos});
// Skip over character just read
$pos++;
// Check if there is another character available
if ( $pos < strlen( $utf16_text ) )
{
// Another character is available - get it for the second half of the UTF-16 value
$chval2 = ord( $utf16_text{$pos} );
}
else
{
// Error - no second byte to this UTF-16 value - end processing
continue 1;
}
// Skip over character just read
$pos++;
// Calculate the 16 bit unicode value
if ( $MSB_first )
{
// Big Endian
$UTF16_val = $chval1 * 0x100 + $chval2;
}
else
{
// Little Endian
$UTF16_val = $chval2 * 0x100 + $chval1;
}
if ( ( ( $UTF16_val >= 0x0000 ) && ( $UTF16_val <= 0xD7FF ) ) ||
( ( $UTF16_val >= 0xE000 ) && ( $UTF16_val <= 0xFFFF ) ) )
{
// Normal Character (Non Surrogate pair)
// Add it to the output
$output .= chr( $chval1 ) . chr ( $chval2 );
}
else if ( ( $UTF16_val >= 0xD800 ) && ( $UTF16_val <= 0xDBFF ) )
{
// High surrogate of a surrogate pair
// Now we need to read the low surrogate
// Check if there is another 2 characters available
if ( ( $pos + 3 ) < strlen( $utf16_text ) )
{
// Another 2 characters are available - get them
$chval3 = ord( $utf16_text{$pos} );
$chval4 = ord( $utf16_text{$pos+1} );
// Calculate the second 16 bit unicode value
if ( $MSB_first )
{
// Big Endian
$UTF16_val2 = $chval3 * 0x100 + $chval4;
}
else
{
// Little Endian
$UTF16_val2 = $chval4 * 0x100 + $chval3;
}
// Check that this is a low surrogate
if ( ( $UTF16_val2 >= 0xDC00 ) && ( $UTF16_val2 <= 0xDFFF ) )
{
// Low surrogate found following high surrogate
// Add both to the output
$output .= chr( $chval1 ) . chr ( $chval2 ) . chr( $chval3 ) . chr ( $chval4 );
// Skip over the low surrogate
$pos += 2;
}
else
{
// Low surrogate not found after high surrogate
// Don't add either to the output
// Only the High surrogate is skipped and processing continues after it
}
}
else
{
// Error - not enough data for low surrogate - end processing
continue 1;
}
}
else
{
// Low surrogate of a surrogate pair
// This should not happen - it means this is a lone low surrogate
// Dont add it to the output
}
}
// Return the result
return $output;
}
/******************************************************************************
* End of Function: UTF16_fix
******************************************************************************/
/******************************************************************************
*
* Function: UTF8_to_unicode_array
*
* Description: Converts a string encoded with Unicode UTF-8, to an array of
* numbers which represent unicode character numbers
*
* Parameters: utf8_text - a string containing the UTF-8 data
*
* Returns: output - the array containing the unicode character numbers
*
******************************************************************************/
function UTF8_to_unicode_array( $utf8_text )
{
// Create an array to receive the unicode character numbers output
$output = array( );
// Cycle through the characters in the UTF-8 string
for ( $pos = 0; $pos < strlen( $utf8_text ); $pos++ )
{
// Retreive the current numerical character value
$chval = ord($utf8_text{$pos});
// Check what the first character is - it will tell us how many bytes the
// Unicode value covers
if ( ( $chval >= 0x00 ) && ( $chval <= 0x7F ) )
{
// 1 Byte UTF-8 Unicode (7-Bit ASCII) Character
$bytes = 1;
$outputval = $chval; // Since 7-bit ASCII is unaffected, the output equals the input
}
else if ( ( $chval >= 0xC0 ) && ( $chval <= 0xDF ) )
{
// 2 Byte UTF-8 Unicode
$bytes = 2;
$outputval = $chval & 0x1F; // The first byte is bitwise ANDed with 0x1F to remove the leading 110b
}
else if ( ( $chval >= 0xE0 ) && ( $chval <= 0xEF ) )
{
// 3 Byte UTF-8 Unicode
$bytes = 3;
$outputval = $chval & 0x0F; // The first byte is bitwise ANDed with 0x0F to remove the leading 1110b
}
else if ( ( $chval >= 0xF0 ) && ( $chval <= 0xF7 ) )
{
// 4 Byte UTF-8 Unicode
$bytes = 4;
$outputval = $chval & 0x07; // The first byte is bitwise ANDed with 0x07 to remove the leading 11110b
}
else if ( ( $chval >= 0xF8 ) && ( $chval <= 0xFB ) )
{
// 5 Byte UTF-8 Unicode
$bytes = 5;
$outputval = $chval & 0x03; // The first byte is bitwise ANDed with 0x03 to remove the leading 111110b
}
else if ( ( $chval >= 0xFC ) && ( $chval <= 0xFD ) )
{
// 6 Byte UTF-8 Unicode
$bytes = 6;
$outputval = $chval & 0x01; // The first byte is bitwise ANDed with 0x01 to remove the leading 1111110b
}
else
{
// Invalid Code - do nothing
$bytes = 0;
}
// Check if the byte was valid
if ( $bytes !== 0 )
{
// The byte was valid
// Check if there is enough data left in the UTF-8 string to allow the
// retrieval of the remainder of this unicode character
if ( $pos + $bytes - 1 < strlen( $utf8_text ) )
{
// The UTF-8 string is long enough
// Cycle through the number of bytes required,
// minus the first one which has already been done
while ( $bytes > 1 )
{
$pos++;
$bytes--;
// Each remaining byte is coded with 6 bits of data and 10b on the high
// order bits. Hence we need to shift left by 6 bits (0x40) then add the
// current characer after it has been bitwise ANDed with 0x3F to remove the
// highest two bits.
$outputval = $outputval*0x40 + ( (ord($utf8_text{$pos})) & 0x3F );
}
// Add the calculated Unicode number to the output array
$output[] = $outputval;
}
}
}
// Return the resulting array
return $output;
}
/******************************************************************************
* End of Function: UTF8_to_unicode_array
******************************************************************************/
/******************************************************************************
*
* Function: UTF16_to_unicode_array
*
* Description: Converts a string encoded with Unicode UTF-16, to an array of
* numbers which represent unicode character numbers
*
* Parameters: utf16_text - a string containing the UTF-16 data
* MSB_first - True will cause processing as Big Endian UTF-16 (Motorola, MSB first)
* False will cause processing as Little Endian UTF-16 (Intel, LSB first)
*
* Returns: output - the array containing the unicode character numbers
*
******************************************************************************/
function UTF16_to_unicode_array( $utf16_text, $MSB_first )
{
// Create an array to receive the unicode character numbers output
$output = array( );
// Initialise the current position in the string
$pos = 0;
// Cycle through each group of bytes, ensuring the coding is correct
while ( $pos < strlen( $utf16_text ) )
{
// Retreive the current numerical character value
$chval1 = ord($utf16_text{$pos});
// Skip over character just read
$pos++;
// Check if there is another character available
if ( $pos < strlen( $utf16_text ) )
{
// Another character is available - get it for the second half of the UTF-16 value
$chval2 = ord( $utf16_text{$pos} );
}
else
{
// Error - no second byte to this UTF-16 value - end processing
continue 1;
}
// Skip over character just read
$pos++;
// Calculate the 16 bit unicode value
if ( $MSB_first )
{
// Big Endian
$UTF16_val = $chval1 * 0x100 + $chval2;
}
else
{
// Little Endian
$UTF16_val = $chval2 * 0x100 + $chval1;
}
if ( ( ( $UTF16_val >= 0x0000 ) && ( $UTF16_val <= 0xD7FF ) ) ||
( ( $UTF16_val >= 0xE000 ) && ( $UTF16_val <= 0xFFFF ) ) )
{
// Normal Character (Non Surrogate pair)
// Add it to the output
$output[] = $UTF16_val;
}
else if ( ( $UTF16_val >= 0xD800 ) && ( $UTF16_val <= 0xDBFF ) )
{
// High surrogate of a surrogate pair
// Now we need to read the low surrogate
// Check if there is another 2 characters available
if ( ( $pos + 3 ) < strlen( $utf16_text ) )
{
// Another 2 characters are available - get them
$chval3 = ord( $utf16_text{$pos} );
$chval4 = ord( $utf16_text{$pos+1} );
// Calculate the second 16 bit unicode value
if ( $MSB_first )
{
// Big Endian
$UTF16_val2 = $chval3 * 0x100 + $chval4;
}
else
{
// Little Endian
$UTF16_val2 = $chval4 * 0x100 + $chval3;
}
// Check that this is a low surrogate
if ( ( $UTF16_val2 >= 0xDC00 ) && ( $UTF16_val2 <= 0xDFFF ) )
{
// Low surrogate found following high surrogate
// Add both to the output
$output[] = 0x10000 + ( ( $UTF16_val - 0xD800 ) * 0x400 ) + ( $UTF16_val2 - 0xDC00 );
// Skip over the low surrogate
$pos += 2;
}
else
{
// Low surrogate not found after high surrogate
// Don't add either to the output
// The high surrogate is skipped and processing continued
}
}
else
{
// Error - not enough data for low surrogate - end processing
continue 1;
}
}
else
{
// Low surrogate of a surrogate pair
// This should not happen - it means this is a lone low surrogate
// Don't add it to the output
}
}
// Return the result
return $output;
}
/******************************************************************************
* End of Function: UTF16_to_unicode_array
******************************************************************************/
/******************************************************************************
*
* Function: unicode_array_to_UTF8
*
* Description: Converts an array of unicode character numbers to a string
* encoded by UTF-8
*
* Parameters: unicode_array - the array containing unicode character numbers
*
* Returns: output - the UTF-8 encoded string representing the data
*
******************************************************************************/
function unicode_array_to_UTF8( $unicode_array )
{
// Create a string to receive the UTF-8 output
$output = "";
// Cycle through each Unicode character number
foreach( $unicode_array as $unicode_char )
{
// Check which range the current unicode character lies in
if ( ( $unicode_char >= 0x00 ) && ( $unicode_char <= 0x7F ) )
{
// 1 Byte UTF-8 Unicode (7-Bit ASCII) Character
$output .= chr($unicode_char); // Output is equal to input for 7-bit ASCII
}
else if ( ( $unicode_char >= 0x80 ) && ( $unicode_char <= 0x7FF ) )
{
// 2 Byte UTF-8 Unicode - binary encode data as : 110xxxxx 10xxxxxx
$output .= chr(0xC0 + ($unicode_char/0x40));
$output .= chr(0x80 + ($unicode_char & 0x3F));
}
else if ( ( $unicode_char >= 0x800 ) && ( $unicode_char <= 0xFFFF ) )
{
// 3 Byte UTF-8 Unicode - binary encode data as : 1110xxxx 10xxxxxx 10xxxxxx
$output .= chr(0xE0 + ($unicode_char/0x1000));
$output .= chr(0x80 + (($unicode_char/0x40) & 0x3F));
$output .= chr(0x80 + ($unicode_char & 0x3F));
}
else if ( ( $unicode_char >= 0x10000 ) && ( $unicode_char <= 0x1FFFFF ) )
{
// 4 Byte UTF-8 Unicode - binary encode data as : 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
$output .= chr(0xF0 + ($unicode_char/0x40000));
$output .= chr(0x80 + (($unicode_char/0x1000) & 0x3F));
$output .= chr(0x80 + (($unicode_char/0x40) & 0x3F));
$output .= chr(0x80 + ($unicode_char & 0x3F));
}
else if ( ( $unicode_char >= 0x200000 ) && ( $unicode_char <= 0x3FFFFFF ) )
{
// 5 Byte UTF-8 Unicode - binary encode data as : 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
$output .= chr(0xF8 + ($unicode_char/0x1000000));
$output .= chr(0x80 + (($unicode_char/0x40000) & 0x3F));
$output .= chr(0x80 + (($unicode_char/0x1000) & 0x3F));
$output .= chr(0x80 + (($unicode_char/0x40) & 0x3F));
$output .= chr(0x80 + ($unicode_char & 0x3F));
}
else if ( ( $unicode_char >= 0x4000000 ) && ( $unicode_char <= 0x7FFFFFFF ) )
{
// 6 Byte UTF-8 Unicode - binary encode data as : 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
$output .= chr(0xFC + ($unicode_char/0x40000000));
$output .= chr(0x80 + (($unicode_char/0x1000000) & 0x3F));
$output .= chr(0x80 + (($unicode_char/0x40000) & 0x3F));
$output .= chr(0x80 + (($unicode_char/0x1000) & 0x3F));
$output .= chr(0x80 + (($unicode_char/0x40) & 0x3F));
$output .= chr(0x80 + ($unicode_char & 0x3F));
}
else
{
// Invalid Code - do nothing
}
}
// Return resulting UTF-8 String
return $output;
}
/******************************************************************************
* End of Function: unicode_array_to_UTF8
******************************************************************************/
/******************************************************************************
*
* Function: unicode_array_to_UTF16
*
* Description: Converts an array of unicode character numbers to a string
* encoded by UTF-16
*
* Parameters: unicode_array - the array containing unicode character numbers
* MSB_first - True will cause processing as Big Endian UTF-16 (Motorola, MSB first)
* False will cause processing as Little Endian UTF-16 (Intel, LSB first)
*
* Returns: output - the UTF-16 encoded string representing the data
*
******************************************************************************/
function unicode_array_to_UTF16( $unicode_array, $MSB_first )
{
// Create a string to receive the UTF-16 output
$output = "";
// Cycle through each Unicode character number
foreach( $unicode_array as $unicode_char )
{
// Check which range the current unicode character lies in
if ( ( ( $unicode_char >= 0x0000 ) && ( $unicode_char <= 0xD7FF ) ) ||
( ( $unicode_char >= 0xE000 ) && ( $unicode_char <= 0xFFFF ) ) )
{
// Normal 16 Bit Character (Not a Surrogate Pair)
// Check what byte order should be used
if ( $MSB_first )
{
// Big Endian
$output .= chr( $unicode_char / 0x100 ) . chr( $unicode_char % 0x100 ) ;
}
else
{
// Little Endian
$output .= chr( $unicode_char % 0x100 ) . chr( $unicode_char / 0x100 ) ;
}
}
else if ( ( $unicode_char >= 0x10000 ) && ( $unicode_char <= 0x10FFFF ) )
{
// Surrogate Pair required
// Calculate Surrogates
$High_Surrogate = ( ( $unicode_char - 0x10000 ) / 0x400 ) + 0xD800;
$Low_Surrogate = ( ( $unicode_char - 0x10000 ) % 0x400 ) + 0xDC00;
// Check what byte order should be used
if ( $MSB_first )
{
// Big Endian
$output .= chr( $High_Surrogate / 0x100 ) . chr( $High_Surrogate % 0x100 );
$output .= chr( $Low_Surrogate / 0x100 ) . chr( $Low_Surrogate % 0x100 );
}
else
{
// Little Endian
$output .= chr( $High_Surrogate % 0x100 ) . chr( $High_Surrogate / 0x100 );
$output .= chr( $Low_Surrogate % 0x100 ) . chr( $Low_Surrogate / 0x100 );
}
}
else
{
// Invalid UTF-16 codepoint
// Unicode value should never be between 0xD800 and 0xDFFF
// Do not output this point - there is no way to encode it in UTF-16
}
}
// Return resulting UTF-16 String
return $output;
}
/******************************************************************************
* End of Function: unicode_array_to_UTF16
******************************************************************************/
/******************************************************************************
*
* Function: xml_UTF8_clean
*
* Description: XML has specific requirements about the characters that are
* allowed, and characters that must be escaped.
* This function ensures that all characters in the given string
* are valid, and that characters such as Quotes, Greater than,
* Less than and Ampersand are properly escaped. Newlines and Tabs
* are also escaped.
* Note - Do not use this on constructed XML which includes tags,
* as it will escape the tags. It is designed to be used
* on the tag and attribute names, attribute values, and text.
*
* Parameters: utf8_text - a string containing the UTF-8 data
*
* Returns: output - the array containing the unicode character numbers
*
******************************************************************************/
function xml_UTF8_clean( $UTF8_text )
{
// Ensure that the Unicode UTF8 encoding is valid.
$UTF8_text = UTF8_fix( $UTF8_text );
// XML only allows characters in the following unicode ranges
// #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
// Hence we need to delete any characters that dont fit this
// Convert the UTF-8 string to an array of unicode character numbers
$unicode_array = UTF8_to_unicode_array( $UTF8_text );
// Create a new array to receive the valid unicode character numbers
$new_unicode_array = array( );
// Cycle through the unicode character numbers
foreach( $unicode_array as $unichar )
{
// Check if the unicode character number is valid for XML
if ( ( $unichar == 0x09 ) ||
( $unichar == 0x0A ) ||
( $unichar == 0x0D ) ||
( ( $unichar >= 0x20 ) && ( $unichar <= 0xD7FF ) ) ||
( ( $unichar >= 0xE000 ) && ( $unichar <= 0xFFFD ) ) ||
( ( $unichar >= 0x10000 ) && ( $unichar <= 0x10FFFF ) ) )
{
// Unicode character is valid for XML - add it to the valid characters array
$new_unicode_array[] = $unichar;
}
}
// Convert the array of valid unicode character numbers back to UTF-8 encoded text
$UTF8_text = unicode_array_to_UTF8( $new_unicode_array );
// Escape any special HTML characters present
$UTF8_text = htmlspecialchars ( $UTF8_text, ENT_QUOTES );
// Escape CR, LF and TAB characters, so that they are kept and not treated as expendable white space
$trans = array( "\x09" => "	", "\x0A" => "
", "\x0D" => "
" );
$UTF8_text = strtr( $UTF8_text, $trans );
// Return the resulting XML valid string
return $UTF8_text;
}
/******************************************************************************
* End of Function: xml_UTF8_clean
******************************************************************************/
/******************************************************************************
*
* Function: xml_UTF16_clean
*
* Description: XML has specific requirements about the characters that are
* allowed, and characters that must be escaped.
* This function ensures that all characters in the given string
* are valid, and that characters such as Quotes, Greater than,
* Less than and Ampersand are properly escaped. Newlines and Tabs
* are also escaped.
* Note - Do not use this on constructed XML which includes tags,
* as it will escape the tags. It is designed to be used
* on the tag and attribute names, attribute values, and text.
*
* Parameters: utf16_text - a string containing the UTF-16 data
* MSB_first - True will cause processing as Big Endian UTF-16 (Motorola, MSB first)
* False will cause processing as Little Endian UTF-16 (Intel, LSB first)
*
* Returns: output - the array containing the unicode character numbers
*
******************************************************************************/
function xml_UTF16_clean( $UTF16_text, $MSB_first )
{
// Ensure that the Unicode UTF16 encoding is valid.
$UTF16_text = UTF16_fix( $UTF16_text, $MSB_first );
// XML only allows characters in the following unicode ranges
// #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
// Hence we need to delete any characters that dont fit this
// Convert the UTF-16 string to an array of unicode character numbers
$unicode_array = UTF16_to_unicode_array( $UTF16_text, $MSB_first );
// Create a new array to receive the valid unicode character numbers
$new_unicode_array = array( );
// Cycle through the unicode character numbers
foreach( $unicode_array as $unichar )
{
// Check if the unicode character number is valid for XML
if ( ( $unichar == 0x09 ) ||
( $unichar == 0x0A ) ||
( $unichar == 0x0D ) ||
( ( $unichar >= 0x20 ) && ( $unichar <= 0xD7FF ) ) ||
( ( $unichar >= 0xE000 ) && ( $unichar <= 0xFFFD ) ) ||
( ( $unichar >= 0x10000 ) && ( $unichar <= 0x10FFFF ) ) )
{
// Unicode character is valid for XML - add it to the valid characters array
$new_unicode_array[] = $unichar;
}
}
// Convert the array of valid unicode character numbers back to UTF-16 encoded text
$UTF16_text = unicode_array_to_UTF16( $new_unicode_array, $MSB_first );
// Escape any special HTML characters present
$UTF16_text = htmlspecialchars ( $UTF16_text, ENT_QUOTES );
// Escape CR, LF and TAB characters, so that they are kept and not treated as expendable white space
$trans = array( "\x09" => "	", "\x0A" => "
", "\x0D" => "
" );
$UTF16_text = strtr( $UTF16_text, $trans );
// Return the resulting XML valid string
return $UTF16_text;
}
/******************************************************************************
* End of Function: xml_UTF16_clean
******************************************************************************/
/******************************************************************************
*
* Function: HTML_UTF8_Escape
*
* Description: A HTML page can display UTF-8 data properly if it has a
* META http-equiv="Content-Type" tag with the content attribute
* including the value: "charset=utf-8".
* Otherwise the ISO-8859-1 character set is usually assumed, and
* Unicode values above 0x7F must be escaped.
* This function takes a UTF-8 encoded string and escapes the
* characters above 0x7F as well as reserved HTML characters such
* as Quotes, Greater than, Less than and Ampersand.
*
* Parameters: utf8_text - a string containing the UTF-8 data
*
* Returns: htmloutput - a string containing the HTML equivalent
*
******************************************************************************/
function HTML_UTF8_Escape( $UTF8_text )
{
// Ensure that the Unicode UTF8 encoding is valid.
$UTF8_text = UTF8_fix( $UTF8_text );
// Change: changed to use smart_htmlspecialchars, so that characters which were already escaped would remain intact, as of revision 1.10
// Escape any special HTML characters present
$UTF8_text = smart_htmlspecialchars( $UTF8_text, ENT_QUOTES );
// Convert the UTF-8 string to an array of unicode character numbers
$unicode_array = UTF8_to_unicode_array( $UTF8_text );
// Create a string to receive the escaped HTML
$htmloutput = "";
// Cycle through the unicode character numbers
foreach( $unicode_array as $unichar )
{
// Check if the character needs to be escaped
if ( ( $unichar >= 0x00 ) && ( $unichar <= 0x7F ) )
{
// Character is less than 0x7F - add it to the html as is