-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStringUtils.php
More file actions
1389 lines (1276 loc) · 49.1 KB
/
StringUtils.php
File metadata and controls
1389 lines (1276 loc) · 49.1 KB
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
/**
* FlorianWolters\Component\Core\StringUtils
*
* PHP Version 5.3
*
* @author Florian Wolters <wolters.fl@gmail.com>
* @copyright 2010-2014 Florian Wolters (http://blog.florianwolters.de)
* @license https://gnu.org/licenses/lgpl.txt LGPL-3.0+
* @link https://github.com/FlorianWolters/PHP-Component-Core-StringUtils
*/
namespace FlorianWolters\Component\Core;
/**
* The class {@see StringUtils} offers operations `string`s.
*
* This class is inspired by the Java class {@link
* https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/StringUtils.html
* StringUtils} from the {@link https://commons.apache.org/lang Apache Commons
* Lang Application Programming Interface (API)}.
*
* @since Class available since Release 0.1.0
*/
final class StringUtils
{
/**
* The empty `string` `''`.
*
* @var string
*/
const EMPTY_STR = '';
/**
* Represents a failed index search.
*
* @var integer
*/
const INDEX_NOT_FOUND = -1;
// @codeCoverageIgnoreStart
/**
* {@see StringUtils} instances can **NOT** be constructed in standard
* programming.
*
* Instead, the class should be used as:
*
* StringUtils::trim(' foo ');
*/
protected function __construct()
{
// NOOP
}
// @codeCoverageIgnoreEnd
/**
* Returns the character at the specified index.
*
* An index ranges from `0` to `StringUtils::length() - 1`. The first
* character of the sequence is at index `0`, the next at index `1`, and so
* on, as for array indexing.
*
* @param string $str The `string` to return the character from.
* @param integer $index The index of the character.
*
* @return string The character at the specified index of the `string`. The
* first character is at index `0`.
* @throws OutOfBoundsException If the $index argument is negative or not
* less than the length of the `string`.
*/
public static function charAt($str, $index)
{
if (false === substr($str, $index, 1)) {
throw new \OutOfBoundsException;
}
return substr($str, $index, 1);
}
/**
* Removes one newline from end of a string if it's there, otherwise leave
* it alone.
*
* A newline is "`\n`", "`\r`", or "`\r\n`".
*
* StringUtils::chomp(null); // null
* StringUtils::chomp(''); // ''
* StringUtils::chomp("abc \r"); // 'abc '
* StringUtils::chomp("abc\n"); // 'abc'
* StringUtils::chomp("abc\r\n"); // 'abc'
* StringUtils::chomp("abc\r\n\r\n"); // "abc\r\n"
* StringUtils::chomp("abc\n\r"); // "abc\n"
* StringUtils::chomp("abc\n\rabc"); // "abc\n\rabc"
* StringUtils::chomp("\r"); // ''
* StringUtils::chomp("\n"); // ''
* StringUtils::chomp("\r\n"); // ''
*
* @param string $str The `string` to chomp a newline from.
*
* @return string The `string` $str without newline, `null` if `null`
* `string` input.
*/
public static function chomp($str)
{
if (true === self::isEmpty($str)) {
return $str;
}
if (1 === self::length($str)) {
$firstChar = self::charAt($str, 0);
if ("\r" === $firstChar || "\n" === $firstChar) {
return self::EMPTY_STR;
}
return $str;
}
$lastIndex = self::length($str) - 1;
$lastChar = self::charAt($str, $lastIndex);
if ("\n" === $lastChar) {
if ("\r" === self::charAt($str, $lastIndex - 1)) {
--$lastIndex;
}
} elseif ("\r" !== $lastChar) {
++$lastIndex;
}
return self::substring($str, 0, $lastIndex);
}
/**
* Remove the specified last character from a `string`.
*
* If the `string` ends in `\r\n`, then remove both of them.
*
* StringUtils::chop(null); // null
* StringUtils::chop(''); // ''
* StringUtils::chop("abc \r"); // 'abc '
* StringUtils::chop("abc\n"); // 'abc'
* StringUtils::chop("abc\r\n"); // 'abc'
* StringUtils::chop('abc'); // 'ab'
* StringUtils::chop("abc\nabc"); // "abc\nab"
* StringUtils::chop('a'); // ''
* StringUtils::chop("\r"); // ''
* StringUtils::chop("\n"); // ''
* StringUtils::chop("\r\n"); // ''
*
* @param string $str The `string` to chop the last character from.
*
* @return string|null The `string` without the last character; `null` if
* `null` `string` input.
*/
public static function chop($str)
{
if (true === self::isEmpty($str)) {
return $str;
}
if ("\r\n" === \substr($str, -2)) {
return \substr($str, 0, -2);
}
return \substr($str, 0, -1);
}
/**
* Checks if a `string` is whitespace, empty (`''`) or `null`.
*
* StringUtils::isBlank(null); // true
* StringUtils::isBlank(''); // true
* StringUtils::isBlank(' '); // true
* StringUtils::isBlank('bob'); // false
* StringUtils::isBlank(' bob '); // false
*
* @param string $str The `string` to check.
*
* @return boolean `true` if the `string` is `null`, empty or whitespace;
* `false` otherwise.
*/
public static function isBlank($str)
{
return self::EMPTY_STR === \trim($str);
}
/**
* Checks if a `string` is not empty (`''`), not `null` and not whitespace
* only.
*
* StringUtils::isNotBlank(null); // false
* StringUtils::isNotBlank(''); // false
* StringUtils::isNotBlank(' '); // false
* StringUtils::isNotBlank('bob'); // true
* StringUtils::isNotBlank(' bob '); // true
*
* @param string $str The `string` to check.
*
* @return boolean `true` if the `string` is not empty and not `null` and
* not whitespace; `false` otherwise.
*/
public static function isNotBlank($str)
{
return false === self::isBlank($str);
}
/**
* Checks if a `string` is empty (`''`) or `null`.
*
* StringUtils::isEmpty(null); // true
* StringUtils::isEmpty(''); // true
* StringUtils::isEmpty(' '); // false
* StringUtils::isEmpty('bob'); // false
* StringUtils::isEmpty(' bob '); // false
*
* @param string $str The `string` to check.
*
* @return boolean `true` if the `string` is empty or `null`, `false`
* otherwise.
*/
public static function isEmpty($str)
{
return (self::EMPTY_STR === $str) || (null === $str);
}
/**
* Checks if a `string` is not empty (`''`) and not `null`.
*
* StringUtils::isNotEmpty(null); // false
* StringUtils::isNotEmpty(''); // false
* StringUtils::isNotEmpty(' '); // true
* StringUtils::isNotEmpty('bob'); // true
* StringUtils::isNotEmpty(' bob '); // true
*
* @param string $str The `string` to check.
*
* @return boolean `true` if the `string` is not empty or `null`, `false`
* otherwise.
*/
public static function isNotEmpty($str)
{
return false === self::isEmpty($str);
}
/**
* Returns the length of a `string` or `0` if the `string` is `null`.
*
* @param string $str The `string` to check.
*
* @return integer The length of the `string` or `0` if the `string` is
* `null`.
*/
public static function length($str)
{
return \strlen($str);
}
/**
* Converts a `string` to lower case.
*
* A `null` input `string` returns `null`.
*
* StringUtils::lowerCase(null) = null
* StringUtils::lowerCase('') = ''
* StringUtils::lowerCase('aBc') = 'abc'
*
* @param string $str The `string` to lower case.
*
* @return string The lower cased `string` or `null` if `null` `string`
* input.
*/
public static function lowerCase($str)
{
return (true === self::isEmpty($str))
? $str
: strtolower($str);
}
/**
* Converts a `string` to upper case.
*
* A `null` input `string` returns `null`.
*
* StringUtils::upperCase(null) = null
* StringUtils::upperCase('') = ''
* StringUtils::upperCase('aBc') = 'ABC'
*
* @param string $str The `string` to upper case.
*
* @return string The upper cased `string` or `null` if `null` `string`
* input.
*/
public static function upperCase($str)
{
return (true === self::isEmpty($str))
? $str
: strtoupper($str);
}
/**
* Replaces a `string` with another `string` inside a larger `string`, for
* the first maximum number of values to replace of the search `string`.
*
* StringUtils::replace(null, *, *, *) // null
* StringUtils::replace('', *, *, *) // ''
* StringUtils::replace('any', null, *, *) // 'any'
* StringUtils::replace('any', *, null, *) // 'any'
* StringUtils::replace('any', '', *, *) // 'any'
* StringUtils::replace('any', *, *, 0) // 'any'
* StringUtils::replace('abaa', 'a', null, -1) // 'abaa'
* StringUtils::replace('abaa', 'a', '', -1) // 'b'
* StringUtils::replace('abaa', 'a', 'z', 0) // 'abaa'
* StringUtils::replace('abaa', 'a', 'z', 1) // 'zbaa'
* StringUtils::replace('abaa', 'a', 'z', 2) // 'zbza'
* StringUtils::replace('abaa', 'a', 'z', -1) // 'zbzz'
*
* @param string $text The `string` to search and replace in.
* @param string $search The `string` to search for.
* @param string $replace The `string` to replace $search with.
* @param integer $max The maximum number of values to replace, or `-1`
* if no maximum.
*
* @return string The text with any replacements processed or `null` if
* `null` `string` input.
*/
public static function replace($text, $search, $replace, $max = -1)
{
if ((true === self::isEmpty($text))
|| (true === self::isEmpty($search))
|| (null === $replace)
|| (0 === $max)
) {
return $text;
}
return \preg_replace(
'/' . \preg_quote($search) . '/',
$replace,
$text,
$max
);
}
/* -------------------------------------------------------------------------
* Trim
* ---------------------------------------------------------------------- */
/**
* Removes control characters (char <= 32) from both ends of a `string`,
* handling `null` by returning `null`.
*
* This method removes start and end characters <= 32. To strip
* whitespace use {@see strip}.
*
* To trim your choice of characters, use the {@see strip} method.
*
* StringUtils::trim(null); // null
* StringUtils::trim(''); // ''
* StringUtils::trim(' '); // ''
* StringUtils::trim('abc'); // 'abc'
* StringUtils::trim(' abc '); // 'abc'
*
* @param string $str The `string` to be trimmed.
*
* @return string The trimmed `string` or `null` if `null` `string` input.
*/
public static function trim($str)
{
return (true === self::isEmpty($str))
? $str
: self::trimToEmpty($str);
}
/**
* Removes control characters (char <= 32) from both ends of a `string`
* returning an empty `string` (`''`) if the `string` is empty (`''`) after
* the trim or if it is `null`.
*
* This method removes start and end characters <= 32. To strip
* whitespace use {@see stripToEmpty}.
*
* StringUtils::trimToEmpty(null); // ''
* StringUtils::trimToEmpty(''); // ''
* StringUtils::trimToEmpty(' '); // ''
* StringUtils::trimToEmpty('abc'); // 'abc'
* StringUtils::trimToEmpty(' abc '); // 'abc'
*
* @param string $str The `string` to be trimmed.
*
* @return string The trimmed `string` or an empty `string` if `null` input.
*/
public static function trimToEmpty($str)
{
return \trim($str);
}
/**
* Removes control characters (char <= 32) from both ends of a `string`
* returning `null` if the `string` is empty (`''`) after the trim or if it
* is `null`.
*
* This method removes start and end characters <= 32. To strip
* whitespace use {@see stripToNull}.
*
* StringUtils::trimToNull(null); // null
* StringUtils::trimToNull(''); // null
* StringUtils::trimToNull(' '); // null
* StringUtils::trimToNull('abc'); // 'abc'
* StringUtils::trimToNull(' abc '); // 'abc'
*
* @param string $str The `string` to be trimmed.
*
* @return string|null The trimmed `string' or `null` if only chars
* <= 32, empty or `null` `string` input.
*/
public static function trimToNull($str)
{
$str = self::trimToEmpty($str);
return (true === self::isEmpty($str))
? null
: $str;
}
/* -------------------------------------------------------------------------
* Strip
* ---------------------------------------------------------------------- */
/**
* Strips any of a set of characters from the start and end of a `string`.
*
* This is similar to {@see trim} but allows the characters to be stripped
* to be controlled.
*
* A `null` input `string` returns `null`.
* An empty string (`''`) input returns the empty `string`.
*
* If the `string` for the characters to remove is `null`, whitespace is
* stripped.
*
* StringUtils::strip(null, *); // null
* StringUtils::strip('', *); // ''
* StringUtils::strip('abc', null); // 'abc'
* StringUtils::strip(' abc', null); // 'abc'
* StringUtils::strip('abc ', null); // 'abc'
* StringUtils::strip(' abc ', null); // 'abc'
* StringUtils::strip(' abcyx', 'xyz'); // ' abc'
*
* @param string $str The `string` to remove characters from.
* @param string $chars The characters to remove. `null` is treated as
* whitespace.
*
* @return string|null The stripped `string` or `null` if `null` `string`
* input.
*/
public static function strip($str, $chars)
{
return (true === self::isEmpty($str))
? $str
: self::stripEnd(self::stripStart($str, $chars), $chars);
}
/**
* Strips whitespace from the start and end of a `string` returning an empty
* `string` if `null` input.
*
* This is similar to {@see trimToEmpty} but removes whitespace.
*
* StringUtils::stripToEmpty(null); // ''
* StringUtils::stripToEmpty(''); // ''
* StringUtils::stripToEmpty(' '); // ''
* StringUtils::stripToEmpty('abc'); // 'abc'
* StringUtils::stripToEmpty(' abc'); // 'abc'
* StringUtils::stripToEmpty('abc '); // 'abc'
* StringUtils::stripToEmpty(' abc '); // 'abc'
* StringUtils::stripToEmpty(' ab c '); // 'ab c'
*
* @param string $str The `string` to be stripped.
*
* @return string The stripped `string` or an empty `string` if `null`
* input.
*/
public static function stripToEmpty($str)
{
return (null === $str)
? self::EMPTY_STR
: self::strip($str, null);
}
/**
* Strips whitespace from the start and end of a `string` returning `null`
* if the `string` is empty (`''`) after the strip.
*
* This is similar to {@see trimToNull} but removes whitespace.
*
* StringUtils::stripToNull(null); // null
* StringUtils::stripToNull(''); // null
* StringUtils::stripToNull(' '); // null
* StringUtils::stripToNull('abc'); // 'abc'
* StringUtils::stripToNull(' abc'); // 'abc'
* StringUtils::stripToNull('abc '); // 'abc'
* StringUtils::stripToNull(' abc '); // 'abc'
* StringUtils::stripToNull(' ab c '); // 'ab c'
*
* @param string $str The `string` to be stripped.
*
* @return string|null The stripped `string` or `null` if whitespace, empty
* or `null` `string` input.
*/
public static function stripToNull($str)
{
$str = self::strip($str, null);
return (0 === self::length($str))
? null
: $str;
}
/**
* Strips any of a set of characters from the start of a `string`.
*
* A `null` input `string` returns `null`.
* An empty string (`''`) input returns the empty `string`.
*
* If the `string` for the characters to remove is `null`, whitespace is
* stripped.
*
* StringUtils::stripStart(null, *); // null
* StringUtils::stripStart('', *); // ''
* StringUtils::stripStart('abc', ''); // 'abc'
* StringUtils::stripStart('abc', null); // 'abc'
* StringUtils::stripStart(' abc', null); // 'abc'
* StringUtils::stripStart('abc ', null); // 'abc '
* StringUtils::stripStart(' abc ', null); // 'abc '
* StringUtils::stripStart('yxabc ', 'xyz'); // 'abc '
*
* @param string $str The `string` to remove characters from.
* @param string $chars The characters to remove. `null` is treated as
* whitespace.
*
* @return string|null The stripped `string` or `null` if `null` `string`
* input.
*/
public static function stripStart($str, $chars)
{
if (true === self::isEmpty($str)) {
return $str;
}
return (null === $chars)
? \ltrim($str)
: \ltrim($str, $chars);
}
/**
* Strips any of a set of characters from the end of a `string`.
*
* A `null` input `string` returns `null`.
* An empty string (`''`) input returns the empty `string`.
*
* If the `string` for the characters to remove is `null`, whitespace is
* stripped.
*
* StringUtils::stripEnd(null, *) = null
* StringUtils::stripEnd('', *) = ''
* StringUtils::stripEnd('abc', '') = 'abc'
* StringUtils::stripEnd('abc', null) = 'abc'
* StringUtils::stripEnd(' abc', null) = ' abc'
* StringUtils::stripEnd('abc ', null) = 'abc'
* StringUtils::stripEnd(' abc ', null) = ' abc'
* StringUtils::stripEnd(' abcyx', 'xyz') = ' abc'
*
* @param string $str The `string` to remove characters from.
* @param string $chars The characters to remove. `null` is treated as
* whitespace.
*
* @return string|null The stripped `string` or `null` if `null` `string`
* input.
*/
public static function stripEnd($str, $chars)
{
if (true === self::isEmpty($str)) {
return $str;
}
return (null === $chars)
? \rtrim($str)
: \rtrim($str, $chars);
}
/* -------------------------------------------------------------------------
* Compare
* ---------------------------------------------------------------------- */
/**
* Compares two `string`s lexicographically.
*
* The comparison is based on the Unicode value of each character in the
* `string`s. The character sequence represented by the first `string` is
* compared lexicographically to the character sequence represented by the
* second `string`. The result is a negative integer if the first `string`
* lexicographically precedes the second `string`. The result is a positive
* integer if the first `string` lexicographically follows the second
* `string`.
*
* This method returns an integer whose sign is that of calling {@see
* compare} with normalized versions of the `string`s.
*
* @param string $str1 The first `string` to be compared.
* @param string $str2 The second `string` to be compared.
*
* @return integer A negative integer, zero, or a positive integer as the
* first `string` is less than, equal to, or greater than the second
* `string`.
*/
public static function compare($str1, $str2)
{
return \strcmp($str1, $str2);
}
/**
* Compares two `string`s lexicographically, ignoring case differences.
*
* This method returns an integer whose sign is that of calling {@see
* compare} with normalized versions of the `string`s.
*
* @param string $str1 The first `string` to be compared.
* @param string $str2 The second `string` to be compared.
*
* @return integer A negative integer, zero, or a positive integer as the
* first `string` is greater than, equal to, or less than the second
* `string`, ignoring case considerations.
*/
public static function compareIgnoreCase($str1, $str2)
{
return \strcasecmp($str2, $str1);
}
/* -------------------------------------------------------------------------
* Equals
* ---------------------------------------------------------------------- */
/**
* Compares two `string`s, returning `true` if they are equal.
*
* `null`s are handled without exceptions. Two `null` references are
* considered to be equal. The comparison is case sensitive.
*
* StringUtils::equals(null, null); // true
* StringUtils::equals(null, 'abc'); // false
* StringUtils::equals('abc', null); // false
* StringUtils::equals('abc', 'abc'); // true
* StringUtils::equals('abc', 'ABC'); // false
*
* @param string $str1 The first `string`.
* @param string $str2 The second `string`.
*
* @return boolean `true` if the `string`s are equal, case sensitive, or
* both `null`.
*/
public static function equal($str1, $str2)
{
return (null === $str1)
? (null === $str2)
: ($str1 === $str2);
}
/**
* Compares two `string`s, returning `true` if they are equal ignoring the
* case.
*
* `null`s are handled without exceptions. Two `null` references are
* considered to be equal. The comparison is case insensitive.
*
* StringUtils::equalsIgnoreCase(null, null); // true
* StringUtils::equalsIgnoreCase(null, 'abc'); // false
* StringUtils::equalsIgnoreCase('abc', null); // false
* StringUtils::equalsIgnoreCase('abc', 'abc'); // true
* StringUtils::equalsIgnoreCase('abc', 'ABC'); // true
*
* @param string $str1 The first `string`.
* @param string $str2 The second `string`.
*
* @return boolean `true` if the `string`s are equal, case insensitive, or
* both `null`.
*/
public static function equalsIgnoreCase($str1, $str2)
{
return (null === $str1)
? (null === $str2)
: (self::lowercase($str1) === self::lowercase($str2));
}
/**
* Finds the first index within a `string` from a start position, handling
* `null`.
*
* A `null` or empty (`''`) `string` will return `-1`.
* A negative start position is treated as zero.
* A start position greater than the string length returns `-1`.
*
* StringUtils::indexOf(null, *, *); // -1
* StringUtils::indexOf(*, null, *); // -1
* StringUtils::indexOf('', '', 0); // 0
* StringUtils::indexOf('aabaabaa', 'a', 0); // 0
* StringUtils::indexOf('aabaabaa', 'b', 0); // 2
* StringUtils::indexOf('aabaabaa', 'ab', 0); // 1
* StringUtils::indexOf('aabaabaa', 'b', 3); // 5
* StringUtils::indexOf('aabaabaa', 'b', 9); // -1
* StringUtils::indexOf('aabaabaa', 'b', -1); // 2
* StringUtils::indexOf('aabaabaa', '', 2); // 2
* StringUtils::indexOf('abc', '', 9); // 3
*
* @param string $str The `string` to check.
* @param string $search The `string` to find.
* @param integer $startPos The start position, negative treated as zero.
*
* @return integer The first index of the search character, `-1` if no match
* or `null` `string` input.
*/
public static function indexOf($str, $search, $startPos = 0)
{
$result = self::validateIndexOf($str, $search, $startPos);
if (true !== $result) {
return $result;
}
if (true === self::isEmpty($search)) {
return $startPos;
}
$pos = \strpos($str, $search, $startPos);
return (false === $pos)
? -1
: $pos;
}
/**
* Helper method for {@see indexOf} and {@see lastIndexOf}.
*
* @param string $str The `string` to check.
* @param string $search The `string` to find.
* @param integer $startPos The start position, negative treated as zero.
*
* @return integer|boolean `-1` if no match or `null` `string` input; `true`
* otherwise.
*/
private static function validateIndexOf($str, $search, &$startPos)
{
if ((null === $str) || (null === $search)) {
return -1;
}
$lengthSearch = self::length($search);
$lengthStr = self::length($str);
if ((0 === $lengthSearch) && ($startPos >= $lengthStr)) {
return $lengthStr;
}
if ($startPos >= $lengthStr) {
return -1;
}
if (0 > $startPos) {
$startPos = 0;
}
return true;
}
/**
* Finds the first index within a `string`, handling `null`.
*
* A `null` `string` will return `-1`.
* A negative start position returns `-1`. An empty (`''`) search `string`
* always matches unless the start position is negative.
* A start position greater than the `string` length searches the whole
* `string`.
*
* StringUtils::lastIndexOf(null, *, *); // -1
* StringUtils::lastIndexOf(*, null, *); // -1
* StringUtils::lastIndexOf('aabaabaa', 'a', 8); // 7
* StringUtils::lastIndexOf('aabaabaa', 'b', 8); // 5
* StringUtils::lastIndexOf('aabaabaa', 'ab', 8); // 4
* StringUtils::lastIndexOf('aabaabaa', 'b', 9); // 5
* StringUtils::lastIndexOf('aabaabaa', 'b', -1); // -1
* StringUtils::lastIndexOf('aabaabaa', 'a', 0); // 0
* StringUtils::lastIndexOf('aabaabaa', 'b', 0); // -1
*
* @param string $str The `string` to check.
* @param string $search The `string` to find.
* @param integer $startPos The start position, negative treated as zero.
*
* @return integer The first index of the search `string`, `-1` if no match
* or null `string` input.
*/
public static function lastIndexOf($str, $search, $startPos = 0)
{
$result = self::validateIndexOf($str, $search, $startPos);
if (true !== $result) {
return $result;
}
if (true === self::isEmpty($search)) {
return $startPos;
}
$pos = \strrpos($str, $search, $startPos);
return (false === $pos)
? -1
: $pos;
}
/* -------------------------------------------------------------------------
* Split
* ---------------------------------------------------------------------- */
/**
* Splits the provided text into an `array` with a maximum length,
* separators specified.
*
* The separator is not included in the returned `string` `array`. Adjacent
* separators are treated as one separator. A `null` input `string` returns
* `null`. A `null` $chars splits on whitespace. If more than $max
* delimited substrings are found, the returned `string` includes all
* characters after the first `$max - 1` returned `string`s (including
* separator characters).
*
* StringUtils::split(null, null, null); // null
* StringUtils::split('', null, null); // []
* StringUtils::split('ab cd ef', null, 0); // ['ab', 'cd', 'ef']
* StringUtils::split('ab cd ef', null, 0); // ['ab', 'cd', 'ef']
* StringUtils::split('ab:cd:ef', ':', 0); // ['ab', 'cd', 'ef']
* StringUtils::split('ab:cd:ef', ':', 2); // ['ab', 'cd:ef']
*
* @param string $str The `string` to parse.
* @param string $chars The characters used as the delimiters, `null`
* splits on whitespace.
* @param integer $max The maximum number of elements to include in the
* `array.` A zero or negative value implies no limit.
*
* @return array|null An `array` of parsed `string`s, `null` if `null`
* `string` input.
*/
public static function split($str, $chars = null, $max = 0)
{
$result = self::EMPTY_STR;
if (null === $str) {
return null;
}
if (self::EMPTY_STR === $str) {
return array();
}
if (null === $chars) {
$result = \preg_split('/\s+/', $str, $max);
} elseif ($max > 0) {
$result = \explode($chars, $str, $max);
} else {
$result = \explode($chars, $str);
}
return $result;
}
/**
* Gets a substring from the specified `string` avoiding exceptions.
*
* A negative start position can be used to start/end *n* characters from
* the end of the `string`.
*
* The returned substring starts with the character in the `$start` position
* and ends before the `$end` position. All position counting is zero-based
* -- i.e., to start at the beginning of the `string` use `$start = 0`.
*
* Negative start and end positions can be used to specify offsets relative
* to the end of the `string`.
*
* If `$start` is not strictly to the left of `$end`, the empty string is
* returned.
*
* StringUtils::substring(null, *); // null
* StringUtils::substring('', *); // ''
* StringUtils::substring('abc', 0); // 'abc'
* StringUtils::substring('abc', 2); // 'c'
* StringUtils::substring('abc', 4); // ''
* StringUtils::substring('abc', -2); // 'bc'
* StringUtils::substring('abc', -4); // 'abc'
* StringUtils::substring(null, *, *); // null
* StringUtils::substring('', * , *); // '';
* StringUtils::substring('abc', 0, 2); // 'ab'
* StringUtils::substring('abc', 2, 0); // ''
* StringUtils::substring('abc', 2, 4); // 'c'
* StringUtils::substring('abc', 4, 6); // ''
* StringUtils::substring('abc', 2, 2); // ''
* StringUtils::substring('abc', -2, -1); // 'b'
* StringUtils::substring('abc', -4, 2); // 'ab'
*
* @param string $str The `string` to get the substring from.
* @param integer $start The position to start from, negative means count
* back from the end of the `string` by this many characters.
* @param integer $end The position to end at (exclusive), negative means
* count back from the end of the `string` by this many characters.
*
* @return string|null The substring from start position to end position,
* `null` if `null` `string` input.
*/
public static function substring($str, $start, $end = null)
{
if ((0 > $start) && (0 < $end)) {
$start = 0;
}
if (null === $end) {
$end = self::length($str);
}
return \substr($str, $start, $end - $start);
}
/**
* Gets the substring after the first occurrence of a separator.
*
* The separator is not returned.
*
* A `null` `string` input will return `null`.
* An empty (`''`) `string` input will return the empty `string`.
* A `null` separator will return the empty `string` if the input `string`
* is not `null`.
*
* If nothing is found, the empty `string` is returned.
*
* StringUtils::substringAfter(null, *); // null
* StringUtils::substringAfter('', *); // ''
* StringUtils::substringAfter(*, null); // ''
* StringUtils::substringAfter('abc', 'a'); // 'bc'
* StringUtils::substringAfter('abcba', 'b'); // 'cba'
* StringUtils::substringAfter('abc', 'c'); // ''
* StringUtils::substringAfter('abc', 'd'); // ''
* StringUtils::substringAfter('abc', ''); // 'abc'
*
* @param string $str The `string` to get a substring from.
* @param string $separator The `string` to search for.
*
* @return string|null The substring after the first occurrence of the
* separator, `null` if `null` `string` input.
*/
public static function substringAfter($str, $separator)
{
if (true === self::isEmpty($str)) {
return $str;
}
if (null === $separator) {
return self::EMPTY_STR;
}
$pos = self::indexOf($str, $separator);
if (self::INDEX_NOT_FOUND === $pos) {
return self::EMPTY_STR;
}
return self::substring($str, $pos + self::length($separator));
}
/**
* Gets the substring after the last occurrence of a separator.
*
* The separator is not returned.
*
* A `null` `string` input will return `null`.
* An empty (`''`) `string` input will return the empty `string`.
* An empty or `null` separator will return the empty `string` if the input
* `string` is not `null`.
*
* If nothing is found, the empty `string` is returned.
*
* StringUtils::substringAfterLast(null, *); // null
* StringUtils::substringAfterLast('', *); // ''
* StringUtils::substringAfterLast(*, ''); // ''
* StringUtils::substringAfterLast(*, null); // ''
* StringUtils::substringAfterLast('abc', 'a'); // 'bc'
* StringUtils::substringAfterLast('abcba', 'b'); // 'a'
* StringUtils::substringAfterLast('abc', 'c'); // ''
* StringUtils::substringAfterLast('a', 'a'); // ''
* StringUtils::substringAfterLast('a', 'z'); // ''
*
* @param string $str The `string` to get a substring from.
* @param string $separator The `string` to search for.
*
* @return string|null The substring after the last occurrence of the
* separator, `null` if `null` `string` input.