1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Signify\SecurityChecker; |
4
|
|
|
|
5
|
|
|
final class StringUtil |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Check whether some string starts with some other string. |
9
|
|
|
* |
10
|
|
|
* @param string $haystack String to check. |
11
|
|
|
* @param string[]|string $needles A string or list of strings to check against. |
12
|
|
|
* @return boolean Returns true if the haystack starts with any one of the needles. |
13
|
|
|
*/ |
14
|
|
|
public static function startsWith(string $haystack, $needles): bool |
15
|
|
|
{ |
16
|
|
|
if (!is_array($needles)) { |
17
|
|
|
$needles = [$needles]; |
18
|
|
|
} |
19
|
|
|
foreach ($needles as $needle) { |
20
|
|
|
if (!strlen($needle) || strpos($haystack, $needle) === 0) { |
21
|
|
|
return true; |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
return false; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Check whether some string ends with some other string. |
29
|
|
|
* |
30
|
|
|
* @param string $haystack String to check. |
31
|
|
|
* @param string[]|string $needles A string or list of strings to check against. |
32
|
|
|
* @return boolean Returns true if the haystack ends with any one of the needles. |
33
|
|
|
*/ |
34
|
|
|
public static function endsWith(string $haystack, $needles): bool |
35
|
|
|
{ |
36
|
|
|
if (!is_array($needles)) { |
37
|
|
|
$needles = [$needles]; |
38
|
|
|
} |
39
|
|
|
foreach ($needles as $needle) { |
40
|
|
|
$length = strlen($needle); |
41
|
|
|
if (!$length || substr($haystack, -$length) === $needle) { |
42
|
|
|
return true; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Remove some strings from the start of some other string. |
50
|
|
|
* |
51
|
|
|
* @param string $haystack Original string. |
52
|
|
|
* @param string[]|string $needles A string or list of strings to remove. |
53
|
|
|
* @return string The new string. |
54
|
|
|
*/ |
55
|
|
|
public static function removeFromStart(string $haystack, $needles): string |
56
|
|
|
{ |
57
|
|
|
if (!is_array($needles)) { |
58
|
|
|
$needles = [$needles]; |
59
|
|
|
} |
60
|
|
|
foreach ($needles as $needle) { |
61
|
|
|
if (self::startsWith($haystack, $needle)) { |
62
|
|
|
$toRemoveLength = strlen($needle); |
63
|
|
|
$newLength = strlen($haystack) - $toRemoveLength; |
64
|
|
|
$haystack = substr($haystack, $toRemoveLength, $newLength); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
return $haystack; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Remove some strings from the end of some other string. |
72
|
|
|
* |
73
|
|
|
* @param string $haystack Original string. |
74
|
|
|
* @param string[]|string $needles A string or list of strings to remove. |
75
|
|
|
* @return string The new string. |
76
|
|
|
*/ |
77
|
|
|
public static function removeFromEnd(string $haystack, $needles): string |
78
|
|
|
{ |
79
|
|
|
if (!is_array($needles)) { |
80
|
|
|
$needles = [$needles]; |
81
|
|
|
} |
82
|
|
|
foreach ($needles as $needle) { |
83
|
|
|
if (self::endsWith($haystack, $needle)) { |
84
|
|
|
$newLength = strlen($haystack) - strlen($needle); |
85
|
|
|
$haystack = substr($haystack, 0, $newLength); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
return $haystack; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|