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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
|
<?php
/**
* Multibyte handling methods.
*
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.I18n
* @since CakePHP(tm) v 1.2.0.6833
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
if (!function_exists('mb_stripos')) {
/**
* Find position of first occurrence of a case-insensitive string.
*
* @param string $haystack The string from which to get the position of the first occurrence of $needle.
* @param string $needle The string to find in $haystack.
* @param integer $offset The position in $haystack to start searching.
* @param string $encoding Character encoding name to use. If it is omitted, internal character encoding is used.
* @return integer|boolean The numeric position of the first occurrence of $needle in the $haystack string, or false
* if $needle is not found.
*/
function mb_stripos($haystack, $needle, $offset = 0, $encoding = null) {
return Multibyte::stripos($haystack, $needle, $offset);
}
}
if (!function_exists('mb_stristr')) {
/**
* Finds first occurrence of a string within another, case insensitive.
*
* @param string $haystack The string from which to get the first occurrence of $needle.
* @param string $needle The string to find in $haystack.
* @param boolean $part Determines which portion of $haystack this function returns.
* If set to true, it returns all of $haystack from the beginning to the first occurrence of $needle.
* If set to false, it returns all of $haystack from the first occurrence of $needle to the end,
* Default value is false.
* @param string $encoding Character encoding name to use. If it is omitted, internal character encoding is used.
* @return string|boolean The portion of $haystack, or false if $needle is not found.
*/
function mb_stristr($haystack, $needle, $part = false, $encoding = null) {
return Multibyte::stristr($haystack, $needle, $part);
}
}
if (!function_exists('mb_strlen')) {
/**
* Get string length.
*
* @param string $string The string being checked for length.
* @param string $encoding Character encoding name to use. If it is omitted, internal character encoding is used.
* @return integer The number of characters in string $string having character encoding encoding.
* A multi-byte character is counted as 1.
*/
function mb_strlen($string, $encoding = null) {
return Multibyte::strlen($string);
}
}
if (!function_exists('mb_strpos')) {
/**
* Find position of first occurrence of a string.
*
* @param string $haystack The string being checked.
* @param string $needle The position counted from the beginning of haystack.
* @param integer $offset The search offset. If it is not specified, 0 is used.
* @param string $encoding Character encoding name to use. If it is omitted, internal character encoding is used.
* @return integer|boolean The numeric position of the first occurrence of $needle in the $haystack string.
* If $needle is not found, it returns false.
*/
function mb_strpos($haystack, $needle, $offset = 0, $encoding = null) {
return Multibyte::strpos($haystack, $needle, $offset);
}
}
if (!function_exists('mb_strrchr')) {
/**
* Finds the last occurrence of a character in a string within another.
*
* @param string $haystack The string from which to get the last occurrence of $needle.
* @param string $needle The string to find in $haystack.
* @param boolean $part Determines which portion of $haystack this function returns.
* If set to true, it returns all of $haystack from the beginning to the last occurrence of $needle.
* If set to false, it returns all of $haystack from the last occurrence of $needle to the end,
* Default value is false.
* @param string $encoding Character encoding name to use. If it is omitted, internal character encoding is used.
* @return string|boolean The portion of $haystack. or false if $needle is not found.
*/
function mb_strrchr($haystack, $needle, $part = false, $encoding = null) {
return Multibyte::strrchr($haystack, $needle, $part);
}
}
if (!function_exists('mb_strrichr')) {
/**
* Finds the last occurrence of a character in a string within another, case insensitive.
*
* @param string $haystack The string from which to get the last occurrence of $needle.
* @param string $needle The string to find in $haystack.
* @param boolean $part Determines which portion of $haystack this function returns.
* If set to true, it returns all of $haystack from the beginning to the last occurrence of $needle.
* If set to false, it returns all of $haystack from the last occurrence of $needle to the end,
* Default value is false.
* @param string $encoding Character encoding name to use. If it is omitted, internal character encoding is used.
* @return string|boolean The portion of $haystack. or false if $needle is not found.
*/
function mb_strrichr($haystack, $needle, $part = false, $encoding = null) {
return Multibyte::strrichr($haystack, $needle, $part);
}
}
if (!function_exists('mb_strripos')) {
/**
* Finds position of last occurrence of a string within another, case insensitive
*
* @param string $haystack The string from which to get the position of the last occurrence of $needle.
* @param string $needle The string to find in $haystack.
* @param integer $offset The position in $haystack to start searching.
* @param string $encoding Character encoding name to use. If it is omitted, internal character encoding is used.
* @return integer|boolean The numeric position of the last occurrence of $needle in the $haystack string,
* or false if $needle is not found.
*/
function mb_strripos($haystack, $needle, $offset = 0, $encoding = null) {
return Multibyte::strripos($haystack, $needle, $offset);
}
}
if (!function_exists('mb_strrpos')) {
/**
* Find position of last occurrence of a string in a string.
*
* @param string $haystack The string being checked, for the last occurrence of $needle.
* @param string $needle The string to find in $haystack.
* @param integer $offset May be specified to begin searching an arbitrary number of characters into the string.
* Negative values will stop searching at an arbitrary point prior to the end of the string.
* @param string $encoding Character encoding name to use. If it is omitted, internal character encoding is used.
* @return integer|boolean The numeric position of the last occurrence of $needle in the $haystack string.
* If $needle is not found, it returns false.
*/
function mb_strrpos($haystack, $needle, $offset = 0, $encoding = null) {
return Multibyte::strrpos($haystack, $needle, $offset);
}
}
if (!function_exists('mb_strstr')) {
/**
* Finds first occurrence of a string within another
*
* @param string $haystack The string from which to get the first occurrence of $needle.
* @param string $needle The string to find in $haystack
* @param boolean $part Determines which portion of $haystack this function returns.
* If set to true, it returns all of $haystack from the beginning to the first occurrence of $needle.
* If set to false, it returns all of $haystack from the first occurrence of $needle to the end,
* Default value is FALSE.
* @param string $encoding Character encoding name to use. If it is omitted, internal character encoding is used.
* @return string|boolean The portion of $haystack, or true if $needle is not found.
*/
function mb_strstr($haystack, $needle, $part = false, $encoding = null) {
return Multibyte::strstr($haystack, $needle, $part);
}
}
if (!function_exists('mb_strtolower')) {
/**
* Make a string lowercase
*
* @param string $string The string being lowercased.
* @param string $encoding Character encoding name to use. If it is omitted, internal character encoding is used.
* @return string with all alphabetic characters converted to lowercase.
*/
function mb_strtolower($string, $encoding = null) {
return Multibyte::strtolower($string);
}
}
if (!function_exists('mb_strtoupper')) {
/**
* Make a string uppercase
*
* @param string $string The string being uppercased.
* @param string $encoding Character encoding name to use. If it is omitted, internal character encoding is used.
* @return string with all alphabetic characters converted to uppercase.
*/
function mb_strtoupper($string, $encoding = null) {
return Multibyte::strtoupper($string);
}
}
if (!function_exists('mb_substr_count')) {
/**
* Count the number of substring occurrences
*
* @param string $haystack The string being checked.
* @param string $needle The string being found.
* @param string $encoding Character encoding name to use. If it is omitted, internal character encoding is used.
* @return integer The number of times the $needle substring occurs in the $haystack string.
*/
function mb_substr_count($haystack, $needle, $encoding = null) {
return Multibyte::substrCount($haystack, $needle);
}
}
if (!function_exists('mb_substr')) {
/**
* Get part of string
*
* @param string $string The string being checked.
* @param integer $start The first position used in $string.
* @param integer $length The maximum length of the returned string.
* @param string $encoding Character encoding name to use. If it is omitted, internal character encoding is used.
* @return string The portion of $string specified by the $string and $length parameters.
*/
function mb_substr($string, $start, $length = null, $encoding = null) {
return Multibyte::substr($string, $start, $length);
}
}
if (!function_exists('mb_encode_mimeheader')) {
/**
* Encode string for MIME header
*
* @param string $str The string being encoded
* @param string $charset specifies the name of the character set in which str is represented in.
* The default value is determined by the current NLS setting (mbstring.language).
* @param string $transfer_encoding specifies the scheme of MIME encoding.
* It should be either "B" (Base64) or "Q" (Quoted-Printable). Falls back to "B" if not given.
* @param string $linefeed specifies the EOL (end-of-line) marker with which
* mb_encode_mimeheader() performs line-folding
* (a » RFC term, the act of breaking a line longer than a certain length into multiple lines.
* The length is currently hard-coded to 74 characters). Falls back to "\r\n" (CRLF) if not given.
* @param integer $indent [definition unknown and appears to have no affect]
* @return string A converted version of the string represented in ASCII.
*/
function mb_encode_mimeheader($str, $charset = 'UTF-8', $transferEncoding = 'B', $linefeed = "\r\n", $indent = 1) {
return Multibyte::mimeEncode($str, $charset, $linefeed);
}
}
/**
* Multibyte handling methods.
*
* @package Cake.I18n
*/
class Multibyte {
/**
* Holds the case folding values
*
* @var array
*/
protected static $_caseFold = array();
/**
* Holds an array of Unicode code point ranges
*
* @var array
*/
protected static $_codeRange = array();
/**
* Holds the current code point range
*
* @var string
*/
protected static $_table = null;
/**
* Converts a multibyte character string
* to the decimal value of the character
*
* @param string $string
* @return array
*/
public static function utf8($string) {
$map = array();
$values = array();
$find = 1;
$length = strlen($string);
for ($i = 0; $i < $length; $i++) {
$value = ord($string[$i]);
if ($value < 128) {
$map[] = $value;
} else {
if (empty($values)) {
$find = ($value < 224) ? 2 : 3;
}
$values[] = $value;
if (count($values) === $find) {
if ($find == 3) {
$map[] = (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64);
} else {
$map[] = (($values[0] % 32) * 64) + ($values[1] % 64);
}
$values = array();
$find = 1;
}
}
}
return $map;
}
/**
* Converts the decimal value of a multibyte character string
* to a string
*
* @param array $array
* @return string
*/
public static function ascii($array) {
$ascii = '';
foreach ($array as $utf8) {
if ($utf8 < 128) {
$ascii .= chr($utf8);
} elseif ($utf8 < 2048) {
$ascii .= chr(192 + (($utf8 - ($utf8 % 64)) / 64));
$ascii .= chr(128 + ($utf8 % 64));
} else {
$ascii .= chr(224 + (($utf8 - ($utf8 % 4096)) / 4096));
$ascii .= chr(128 + ((($utf8 % 4096) - ($utf8 % 64)) / 64));
$ascii .= chr(128 + ($utf8 % 64));
}
}
return $ascii;
}
/**
* Find position of first occurrence of a case-insensitive string.
*
* @param string $haystack The string from which to get the position of the first occurrence of $needle.
* @param string $needle The string to find in $haystack.
* @param integer $offset The position in $haystack to start searching.
* @return integer|boolean The numeric position of the first occurrence of $needle in the $haystack string,
* or false if $needle is not found.
*/
public static function stripos($haystack, $needle, $offset = 0) {
if (Multibyte::checkMultibyte($haystack)) {
$haystack = Multibyte::strtoupper($haystack);
$needle = Multibyte::strtoupper($needle);
return Multibyte::strpos($haystack, $needle, $offset);
}
return stripos($haystack, $needle, $offset);
}
/**
* Finds first occurrence of a string within another, case insensitive.
*
* @param string $haystack The string from which to get the first occurrence of $needle.
* @param string $needle The string to find in $haystack.
* @param boolean $part Determines which portion of $haystack this function returns.
* If set to true, it returns all of $haystack from the beginning to the first occurrence of $needle.
* If set to false, it returns all of $haystack from the first occurrence of $needle to the end,
* Default value is false.
* @return integer|boolean The portion of $haystack, or false if $needle is not found.
*/
public static function stristr($haystack, $needle, $part = false) {
$php = (PHP_VERSION < 5.3);
if (($php && $part) || Multibyte::checkMultibyte($haystack)) {
$check = Multibyte::strtoupper($haystack);
$check = Multibyte::utf8($check);
$found = false;
$haystack = Multibyte::utf8($haystack);
$haystackCount = count($haystack);
$needle = Multibyte::strtoupper($needle);
$needle = Multibyte::utf8($needle);
$needleCount = count($needle);
$parts = array();
$position = 0;
while (($found === false) && ($position < $haystackCount)) {
if (isset($needle[0]) && $needle[0] === $check[$position]) {
for ($i = 1; $i < $needleCount; $i++) {
if ($needle[$i] !== $check[$position + $i]) {
break;
}
}
if ($i === $needleCount) {
$found = true;
}
}
if (!$found) {
$parts[] = $haystack[$position];
unset($haystack[$position]);
}
$position++;
}
if ($found && $part && !empty($parts)) {
return Multibyte::ascii($parts);
} elseif ($found && !empty($haystack)) {
return Multibyte::ascii($haystack);
}
return false;
}
if (!$php) {
return stristr($haystack, $needle, $part);
}
return stristr($haystack, $needle);
}
/**
* Get string length.
*
* @param string $string The string being checked for length.
* @return integer The number of characters in string $string
*/
public static function strlen($string) {
if (Multibyte::checkMultibyte($string)) {
$string = Multibyte::utf8($string);
return count($string);
}
return strlen($string);
}
/**
* Find position of first occurrence of a string.
*
* @param string $haystack The string being checked.
* @param string $needle The position counted from the beginning of haystack.
* @param integer $offset The search offset. If it is not specified, 0 is used.
* @return integer|boolean The numeric position of the first occurrence of $needle in the $haystack string.
* If $needle is not found, it returns false.
*/
public static function strpos($haystack, $needle, $offset = 0) {
if (Multibyte::checkMultibyte($haystack)) {
$found = false;
$haystack = Multibyte::utf8($haystack);
$haystackCount = count($haystack);
$needle = Multibyte::utf8($needle);
$needleCount = count($needle);
$position = $offset;
while (($found === false) && ($position < $haystackCount)) {
if (isset($needle[0]) && $needle[0] === $haystack[$position]) {
for ($i = 1; $i < $needleCount; $i++) {
if ($needle[$i] !== $haystack[$position + $i]) {
break;
}
}
if ($i === $needleCount) {
$found = true;
$position--;
}
}
$position++;
}
if ($found) {
return $position;
}
return false;
}
return strpos($haystack, $needle, $offset);
}
/**
* Finds the last occurrence of a character in a string within another.
*
* @param string $haystack The string from which to get the last occurrence of $needle.
* @param string $needle The string to find in $haystack.
* @param boolean $part Determines which portion of $haystack this function returns.
* If set to true, it returns all of $haystack from the beginning to the last occurrence of $needle.
* If set to false, it returns all of $haystack from the last occurrence of $needle to the end,
* Default value is false.
* @return string|boolean The portion of $haystack. or false if $needle is not found.
*/
public static function strrchr($haystack, $needle, $part = false) {
$check = Multibyte::utf8($haystack);
$found = false;
$haystack = Multibyte::utf8($haystack);
$haystackCount = count($haystack);
$matches = array_count_values($check);
$needle = Multibyte::utf8($needle);
$needleCount = count($needle);
$parts = array();
$position = 0;
while (($found === false) && ($position < $haystackCount)) {
if (isset($needle[0]) && $needle[0] === $check[$position]) {
for ($i = 1; $i < $needleCount; $i++) {
if ($needle[$i] !== $check[$position + $i]) {
if ($needle[$i] === $check[($position + $i) - 1]) {
$found = true;
}
unset($parts[$position - 1]);
$haystack = array_merge(array($haystack[$position]), $haystack);
break;
}
}
if (isset($matches[$needle[0]]) && $matches[$needle[0]] > 1) {
$matches[$needle[0]] = $matches[$needle[0]] - 1;
} elseif ($i === $needleCount) {
$found = true;
}
}
if (!$found && isset($haystack[$position])) {
$parts[] = $haystack[$position];
unset($haystack[$position]);
}
$position++;
}
if ($found && $part && !empty($parts)) {
return Multibyte::ascii($parts);
} elseif ($found && !empty($haystack)) {
return Multibyte::ascii($haystack);
}
return false;
}
/**
* Finds the last occurrence of a character in a string within another, case insensitive.
*
* @param string $haystack The string from which to get the last occurrence of $needle.
* @param string $needle The string to find in $haystack.
* @param boolean $part Determines which portion of $haystack this function returns.
* If set to true, it returns all of $haystack from the beginning to the last occurrence of $needle.
* If set to false, it returns all of $haystack from the last occurrence of $needle to the end,
* Default value is false.
* @return string|boolean The portion of $haystack. or false if $needle is not found.
*/
public static function strrichr($haystack, $needle, $part = false) {
$check = Multibyte::strtoupper($haystack);
$check = Multibyte::utf8($check);
$found = false;
$haystack = Multibyte::utf8($haystack);
$haystackCount = count($haystack);
$matches = array_count_values($check);
$needle = Multibyte::strtoupper($needle);
$needle = Multibyte::utf8($needle);
$needleCount = count($needle);
$parts = array();
$position = 0;
while (($found === false) && ($position < $haystackCount)) {
if (isset($needle[0]) && $needle[0] === $check[$position]) {
for ($i = 1; $i < $needleCount; $i++) {
if ($needle[$i] !== $check[$position + $i]) {
if ($needle[$i] === $check[($position + $i) - 1]) {
$found = true;
}
unset($parts[$position - 1]);
$haystack = array_merge(array($haystack[$position]), $haystack);
break;
}
}
if (isset($matches[$needle[0]]) && $matches[$needle[0]] > 1) {
$matches[$needle[0]] = $matches[$needle[0]] - 1;
} elseif ($i === $needleCount) {
$found = true;
}
}
if (!$found && isset($haystack[$position])) {
$parts[] = $haystack[$position];
unset($haystack[$position]);
}
$position++;
}
if ($found && $part && !empty($parts)) {
return Multibyte::ascii($parts);
} elseif ($found && !empty($haystack)) {
return Multibyte::ascii($haystack);
}
return false;
}
/**
* Finds position of last occurrence of a string within another, case insensitive
*
* @param string $haystack The string from which to get the position of the last occurrence of $needle.
* @param string $needle The string to find in $haystack.
* @param integer $offset The position in $haystack to start searching.
* @return integer|boolean The numeric position of the last occurrence of $needle in the $haystack string,
* or false if $needle is not found.
*/
public static function strripos($haystack, $needle, $offset = 0) {
if (Multibyte::checkMultibyte($haystack)) {
$found = false;
$haystack = Multibyte::strtoupper($haystack);
$haystack = Multibyte::utf8($haystack);
$haystackCount = count($haystack);
$matches = array_count_values($haystack);
$needle = Multibyte::strtoupper($needle);
$needle = Multibyte::utf8($needle);
$needleCount = count($needle);
$position = $offset;
while (($found === false) && ($position < $haystackCount)) {
if (isset($needle[0]) && $needle[0] === $haystack[$position]) {
for ($i = 1; $i < $needleCount; $i++) {
if ($needle[$i] !== $haystack[$position + $i]) {
if ($needle[$i] === $haystack[($position + $i) - 1]) {
$position--;
$found = true;
continue;
}
}
}
if (!$offset && isset($matches[$needle[0]]) && $matches[$needle[0]] > 1) {
$matches[$needle[0]] = $matches[$needle[0]] - 1;
} elseif ($i === $needleCount) {
$found = true;
$position--;
}
}
$position++;
}
return ($found) ? $position : false;
}
return strripos($haystack, $needle, $offset);
}
/**
* Find position of last occurrence of a string in a string.
*
* @param string $haystack The string being checked, for the last occurrence of $needle.
* @param string $needle The string to find in $haystack.
* @param integer $offset May be specified to begin searching an arbitrary number of characters into the string.
* Negative values will stop searching at an arbitrary point prior to the end of the string.
* @return integer|boolean The numeric position of the last occurrence of $needle in the $haystack string.
* If $needle is not found, it returns false.
*/
public static function strrpos($haystack, $needle, $offset = 0) {
if (Multibyte::checkMultibyte($haystack)) {
$found = false;
$haystack = Multibyte::utf8($haystack);
$haystackCount = count($haystack);
$matches = array_count_values($haystack);
$needle = Multibyte::utf8($needle);
$needleCount = count($needle);
$position = $offset;
while (($found === false) && ($position < $haystackCount)) {
if (isset($needle[0]) && $needle[0] === $haystack[$position]) {
for ($i = 1; $i < $needleCount; $i++) {
if ($needle[$i] !== $haystack[$position + $i]) {
if ($needle[$i] === $haystack[($position + $i) - 1]) {
$position--;
$found = true;
continue;
}
}
}
if (!$offset && isset($matches[$needle[0]]) && $matches[$needle[0]] > 1) {
$matches[$needle[0]] = $matches[$needle[0]] - 1;
} elseif ($i === $needleCount) {
$found = true;
$position--;
}
}
$position++;
}
return ($found) ? $position : false;
}
return strrpos($haystack, $needle, $offset);
}
/**
* Finds first occurrence of a string within another
*
* @param string $haystack The string from which to get the first occurrence of $needle.
* @param string $needle The string to find in $haystack
* @param boolean $part Determines which portion of $haystack this function returns.
* If set to true, it returns all of $haystack from the beginning to the first occurrence of $needle.
* If set to false, it returns all of $haystack from the first occurrence of $needle to the end,
* Default value is FALSE.
* @return string|boolean The portion of $haystack, or true if $needle is not found.
*/
public static function strstr($haystack, $needle, $part = false) {
$php = (PHP_VERSION < 5.3);
if (($php && $part) || Multibyte::checkMultibyte($haystack)) {
$check = Multibyte::utf8($haystack);
$found = false;
$haystack = Multibyte::utf8($haystack);
$haystackCount = count($haystack);
$needle = Multibyte::utf8($needle);
$needleCount = count($needle);
$parts = array();
$position = 0;
while (($found === false) && ($position < $haystackCount)) {
if (isset($needle[0]) && $needle[0] === $check[$position]) {
for ($i = 1; $i < $needleCount; $i++) {
if ($needle[$i] !== $check[$position + $i]) {
break;
}
}
if ($i === $needleCount) {
$found = true;
}
}
if (!$found) {
$parts[] = $haystack[$position];
unset($haystack[$position]);
}
$position++;
}
if ($found && $part && !empty($parts)) {
return Multibyte::ascii($parts);
} elseif ($found && !empty($haystack)) {
return Multibyte::ascii($haystack);
}
return false;
}
if (!$php) {
return strstr($haystack, $needle, $part);
}
return strstr($haystack, $needle);
}
/**
* Make a string lowercase
*
* @param string $string The string being lowercased.
* @return string with all alphabetic characters converted to lowercase.
*/
public static function strtolower($string) {
$utf8Map = Multibyte::utf8($string);
$length = count($utf8Map);
$lowerCase = array();
for ($i = 0; $i < $length; $i++) {
$char = $utf8Map[$i];
if ($char < 128) {
$str = strtolower(chr($char));
$strlen = strlen($str);
for ($ii = 0; $ii < $strlen; $ii++) {
$lower = ord(substr($str, $ii, 1));
}
$lowerCase[] = $lower;
$matched = true;
} else {
$matched = false;
$keys = self::_find($char, 'upper');
if (!empty($keys)) {
foreach ($keys as $key => $value) {
if ($keys[$key]['upper'] == $char && count($keys[$key]['lower'][0]) === 1) {
$lowerCase[] = $keys[$key]['lower'][0];
$matched = true;
break 1;
}
}
}
}
if ($matched === false) {
$lowerCase[] = $char;
}
}
return Multibyte::ascii($lowerCase);
}
/**
* Make a string uppercase
*
* @param string $string The string being uppercased.
* @return string with all alphabetic characters converted to uppercase.
*/
public static function strtoupper($string) {
$utf8Map = Multibyte::utf8($string);
$length = count($utf8Map);
$replaced = array();
$upperCase = array();
for ($i = 0; $i < $length; $i++) {
$char = $utf8Map[$i];
if ($char < 128) {
$str = strtoupper(chr($char));
$strlen = strlen($str);
for ($ii = 0; $ii < $strlen; $ii++) {
$upper = ord(substr($str, $ii, 1));
}
$upperCase[] = $upper;
$matched = true;
} else {
$matched = false;
$keys = self::_find($char);
$keyCount = count($keys);
if (!empty($keys)) {
foreach ($keys as $key => $value) {
$matched = false;
$replace = 0;
if ($length > 1 && count($keys[$key]['lower']) > 1) {
$j = 0;
for ($ii = 0, $count = count($keys[$key]['lower']); $ii < $count; $ii++) {
$nextChar = $utf8Map[$i + $ii];
if (isset($nextChar) && ($nextChar == $keys[$key]['lower'][$j + $ii])) {
$replace++;
}
}
if ($replace == $count) {
$upperCase[] = $keys[$key]['upper'];
$replaced = array_merge($replaced, array_values($keys[$key]['lower']));
$matched = true;
break 1;
}
} elseif ($length > 1 && $keyCount > 1) {
$j = 0;
for ($ii = 1; $ii < $keyCount; $ii++) {
$nextChar = $utf8Map[$i + $ii - 1];
if (in_array($nextChar, $keys[$ii]['lower'])) {
for ($jj = 0, $count = count($keys[$ii]['lower']); $jj < $count; $jj++) {
$nextChar = $utf8Map[$i + $jj];
if (isset($nextChar) && ($nextChar == $keys[$ii]['lower'][$j + $jj])) {
$replace++;
}
}
if ($replace == $count) {
$upperCase[] = $keys[$ii]['upper'];
$replaced = array_merge($replaced, array_values($keys[$ii]['lower']));
$matched = true;
break 2;
}
}
}
}
if ($keys[$key]['lower'][0] == $char) {
$upperCase[] = $keys[$key]['upper'];
$matched = true;
break 1;
}
}
}
}
if ($matched === false && !in_array($char, $replaced, true)) {
$upperCase[] = $char;
}
}
return Multibyte::ascii($upperCase);
}
/**
* Count the number of substring occurrences
*
* @param string $haystack The string being checked.
* @param string $needle The string being found.
* @return integer The number of times the $needle substring occurs in the $haystack string.
*/
public static function substrCount($haystack, $needle) {
$count = 0;
$haystack = Multibyte::utf8($haystack);
$haystackCount = count($haystack);
$matches = array_count_values($haystack);
$needle = Multibyte::utf8($needle);
$needleCount = count($needle);
if ($needleCount === 1 && isset($matches[$needle[0]])) {
return $matches[$needle[0]];
}
for ($i = 0; $i < $haystackCount; $i++) {
if (isset($needle[0]) && $needle[0] === $haystack[$i]) {
for ($ii = 1; $ii < $needleCount; $ii++) {
if ($needle[$ii] === $haystack[$i + 1]) {
if ((isset($needle[$ii + 1]) && $haystack[$i + 2]) && $needle[$ii + 1] !== $haystack[$i + 2]) {
$count--;
} else {
$count++;
}
}
}
}
}
return $count;
}
/**
* Get part of string
*
* @param string $string The string being checked.
* @param integer $start The first position used in $string.
* @param integer $length The maximum length of the returned string.
* @return string The portion of $string specified by the $string and $length parameters.
*/
public static function substr($string, $start, $length = null) {
if ($start === 0 && $length === null) {
return $string;
}
$string = Multibyte::utf8($string);
for ($i = 1; $i <= $start; $i++) {
unset($string[$i - 1]);
}
if ($length === null || count($string) < $length) {
return Multibyte::ascii($string);
}
$string = array_values($string);
$value = array();
for ($i = 0; $i < $length; $i++) {
$value[] = $string[$i];
}
return Multibyte::ascii($value);
}
/**
* Prepare a string for mail transport, using the provided encoding
*
* @param string $string value to encode
* @param string $charset charset to use for encoding. defaults to UTF-8
* @param string $newline
* @return string
* @TODO: add support for 'Q'('Quoted Printable') encoding
*/
public static function mimeEncode($string, $charset = null, $newline = "\r\n") {
if (!Multibyte::checkMultibyte($string) && strlen($string) < 75) {
return $string;
}
if (empty($charset)) {
$charset = Configure::read('App.encoding');
}
$charset = strtoupper($charset);
$start = '=?' . $charset . '?B?';
$end = '?=';
$spacer = $end . $newline . ' ' . $start;
$length = 75 - strlen($start) - strlen($end);
$length = $length - ($length % 4);
if ($charset == 'UTF-8') {
$parts = array();
$maxchars = floor(($length * 3) / 4);
while (strlen($string) > $maxchars) {
$i = (int)$maxchars;
$test = ord($string[$i]);
while ($test >= 128 && $test <= 191) {
$i--;
$test = ord($string[$i]);
}
$parts[] = base64_encode(substr($string, 0, $i));
$string = substr($string, $i);
}
$parts[] = base64_encode($string);
$string = implode($spacer, $parts);
} else {
$string = chunk_split(base64_encode($string), $length, $spacer);
$string = preg_replace('/' . preg_quote($spacer) . '$/', '', $string);
}
return $start . $string . $end;
}
/**
* Return the Code points range for Unicode characters
*
* @param integer $decimal
* @return string
*/
protected static function _codepoint($decimal) {
if ($decimal > 128 && $decimal < 256) {
$return = '0080_00ff'; // Latin-1 Supplement
} elseif ($decimal < 384) {
$return = '0100_017f'; // Latin Extended-A
} elseif ($decimal < 592) {
$return = '0180_024F'; // Latin Extended-B
} elseif ($decimal < 688) {
$return = '0250_02af'; // IPA Extensions
} elseif ($decimal >= 880 && $decimal < 1024) {
$return = '0370_03ff'; // Greek and Coptic
} elseif ($decimal < 1280) {
$return = '0400_04ff'; // Cyrillic
} elseif ($decimal < 1328) {
$return = '0500_052f'; // Cyrillic Supplement
} elseif ($decimal < 1424) {
$return = '0530_058f'; // Armenian
} elseif ($decimal >= 7680 && $decimal < 7936) {
$return = '1e00_1eff'; // Latin Extended Additional
} elseif ($decimal < 8192) {
$return = '1f00_1fff'; // Greek Extended
} elseif ($decimal >= 8448 && $decimal < 8528) {
$return = '2100_214f'; // Letterlike Symbols
} elseif ($decimal < 8592) {
$return = '2150_218f'; // Number Forms
} elseif ($decimal >= 9312 && $decimal < 9472) {
$return = '2460_24ff'; // Enclosed Alphanumerics
} elseif ($decimal >= 11264 && $decimal < 11360) {
$return = '2c00_2c5f'; // Glagolitic
} elseif ($decimal < 11392) {
$return = '2c60_2c7f'; // Latin Extended-C
} elseif ($decimal < 11520) {
$return = '2c80_2cff'; // Coptic
} elseif ($decimal >= 65280 && $decimal < 65520) {
$return = 'ff00_ffef'; // Halfwidth and Fullwidth Forms
} else {
$return = false;
}
self::$_codeRange[$decimal] = $return;
return $return;
}
/**
* Find the related code folding values for $char
*
* @param integer $char decimal value of character
* @param string $type
* @return array
*/
protected static function _find($char, $type = 'lower') {
$found = array();
if (!isset(self::$_codeRange[$char])) {
$range = self::_codepoint($char);
if ($range === false) {
return null;
}
if (!Configure::configured('_cake_core_')) {
App::uses('PhpReader', 'Configure');
Configure::config('_cake_core_', new PhpReader(CAKE . 'Config' . DS));
}
Configure::load('unicode' . DS . 'casefolding' . DS . $range, '_cake_core_');
self::$_caseFold[$range] = Configure::read($range);
Configure::delete($range);
}
if (!self::$_codeRange[$char]) {
return null;
}
self::$_table = self::$_codeRange[$char];
$count = count(self::$_caseFold[self::$_table]);
for ($i = 0; $i < $count; $i++) {
if ($type === 'lower' && self::$_caseFold[self::$_table][$i][$type][0] === $char) {
$found[] = self::$_caseFold[self::$_table][$i];
} elseif ($type === 'upper' && self::$_caseFold[self::$_table][$i][$type] === $char) {
$found[] = self::$_caseFold[self::$_table][$i];
}
}
return $found;
}
/**
* Check the $string for multibyte characters
* @param string $string value to test
* @return boolean
*/
public static function checkMultibyte($string) {
$length = strlen($string);
for ($i = 0; $i < $length; $i++ ) {
$value = ord(($string[$i]));
if ($value > 128) {
return true;
}
}
return false;
}
}
|