|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EdwinLuijten\Ekko\Broadcast; |
|
4
|
|
|
|
|
5
|
|
|
class StrUtil |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @param $haystack |
|
9
|
|
|
* @param $needles |
|
10
|
|
|
* @return bool |
|
11
|
|
|
*/ |
|
12
|
|
|
public static function contains($haystack, $needles) |
|
13
|
|
|
{ |
|
14
|
|
|
foreach ((array)$needles as $needle) { |
|
15
|
|
|
if ($needle != '' && mb_strpos($haystack, $needle) !== false) { |
|
16
|
|
|
return true; |
|
17
|
|
|
} |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
return false; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param $haystack |
|
25
|
|
|
* @param $needles |
|
26
|
|
|
* @return bool |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function startsWith($haystack, $needles) |
|
29
|
|
|
{ |
|
30
|
|
|
foreach ((array)$needles as $needle) { |
|
31
|
|
|
if ($needle != '' && mb_strpos($haystack, $needle) === 0) { |
|
32
|
|
|
return true; |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return false; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param $haystack |
|
41
|
|
|
* @param $needles |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function endsWith($haystack, $needles) |
|
45
|
|
|
{ |
|
46
|
|
|
foreach ((array)$needles as $needle) { |
|
47
|
|
|
if ((string)$needle === static::substr($haystack, -static::length($needle))) { |
|
48
|
|
|
return true; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return false; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param $string |
|
57
|
|
|
* @param $start |
|
58
|
|
|
* @param null $length |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function substr($string, $start, $length = null) |
|
62
|
|
|
{ |
|
63
|
|
|
return mb_substr($string, $start, $length, 'UTF-8'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param $pattern |
|
68
|
|
|
* @param $value |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function is($pattern, $value) |
|
72
|
|
|
{ |
|
73
|
|
|
if ($pattern == $value) { |
|
74
|
|
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
$pattern = preg_quote($pattern, '#'); |
|
77
|
|
|
|
|
78
|
|
|
// Asterisks are translated into zero-or-more regular expression wildcards |
|
79
|
|
|
// to make it convenient to check if the strings starts with the given |
|
80
|
|
|
// pattern such as "library/*", making any string check convenient. |
|
81
|
|
|
$pattern = str_replace('\*', '.*', $pattern); |
|
82
|
|
|
|
|
83
|
|
|
return (bool)preg_match('#^' . $pattern . '\z#u', $value); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param $value |
|
88
|
|
|
* @return int |
|
89
|
|
|
*/ |
|
90
|
|
|
public static function length($value) |
|
91
|
|
|
{ |
|
92
|
|
|
return mb_strlen($value); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|