|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace luya\helpers; |
|
4
|
|
|
|
|
5
|
|
|
use yii\helpers\BaseStringHelper; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Helper methods when dealing with Strings. |
|
9
|
|
|
* |
|
10
|
|
|
* Extends the {{yii\helpers\StringHelper}} class by some usefull functions like: |
|
11
|
|
|
* |
|
12
|
|
|
* + {{luya\helpers\StringHelper::typeCast()}} |
|
13
|
|
|
* + {{luya\helpers\StringHelper::isFloat()}} |
|
14
|
|
|
* + {{luya\helpers\StringHelper::replaceFirst()}} |
|
15
|
|
|
* + {{luya\helpers\StringHelper::contains()}} |
|
16
|
|
|
* + {{luya\helpers\StringHelper::startsWithWildcard()}} |
|
17
|
|
|
* + {{luya\helpers\StringHelper::typeCastNumeric()}} |
|
18
|
|
|
* |
|
19
|
|
|
* @author Basil Suter <[email protected]> |
|
20
|
|
|
* @since 1.0.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class StringHelper extends BaseStringHelper |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* TypeCast a string to its specific types. |
|
26
|
|
|
* |
|
27
|
|
|
* Arrays will passed to to the {{luya\helpers\ArrayHelper::typeCast()}} class. |
|
28
|
|
|
* |
|
29
|
|
|
* @param mixed $string The input string to type cast. Arrays will be passted to {{luya\helpers\ArrayHelper::typeCast()}}. |
|
30
|
|
|
* @return mixed The new type casted value, if the input is an array the output is the typecasted array. |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function typeCast($string) |
|
33
|
|
|
{ |
|
34
|
|
|
if (is_numeric($string)) { |
|
35
|
|
|
return static::typeCastNumeric($string); |
|
36
|
|
|
} elseif (is_array($string)) { |
|
37
|
|
|
return ArrayHelper::typeCast($string); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $string; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Checke whether a strings starts with the wildcard symbole and compares the string before the wild card symbol * |
|
45
|
|
|
* with the string provided, if there is NO wildcard symbold it always return false. |
|
46
|
|
|
* |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $string The string which should be checked with $with comperator |
|
49
|
|
|
* @param string $with The with string which must end with the wildcard symbol * e.g. `foo*` would match string `foobar`. |
|
50
|
|
|
* @param boolean $caseSensitive Whether to compare the starts with string as case sensitive or not, defaults to true. |
|
51
|
|
|
* @return boolean Whether the string starts with the wildcard marked string or not, if no wildcard symbol is contained. |
|
52
|
|
|
* in the $with it always returns false. |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function startsWithWildcard($string, $with, $caseSensitive = true) |
|
55
|
|
|
{ |
|
56
|
|
|
if (substr($with, -1) != "*") { |
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return self::startsWith($string, rtrim($with, '*'), $caseSensitive); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* TypeCast a numeric value to float or integer. |
|
65
|
|
|
* |
|
66
|
|
|
* If the given value is not a numeric or float value it will be returned as it is. In order to find out whether its float |
|
67
|
|
|
* or not use {{luya\helpers\StringHelper::isFloat()}}. |
|
68
|
|
|
* |
|
69
|
|
|
* @param mixed $value The given value to parse. |
|
70
|
|
|
* @return mixed Returns the original value if not numeric or integer, float casted value. |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function typeCastNumeric($value) |
|
73
|
|
|
{ |
|
74
|
|
|
if (!self::isFloat($value)) { |
|
75
|
|
|
return $value; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (intval($value) == $value) { |
|
79
|
|
|
return (int) $value; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return (float) $value; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Checks whether a string is a float value. |
|
87
|
|
|
* |
|
88
|
|
|
* Compared to `is_float` function of php, it only ensures whether the input variable is type float. |
|
89
|
|
|
* |
|
90
|
|
|
* @param mixed $value The value to check whether its float or not. |
|
91
|
|
|
* @return boolean Whether its a float value or not. |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function isFloat($value) |
|
94
|
|
|
{ |
|
95
|
|
|
if (is_float($value)) { |
|
96
|
|
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return ($value == (string)(float) $value); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Replace only the first occurance found inside the string. |
|
104
|
|
|
* |
|
105
|
|
|
* The replace first method is *case sensitive*. |
|
106
|
|
|
* |
|
107
|
|
|
* ```php |
|
108
|
|
|
* StringHelper::replaceFirst('abc', '123', 'abc abc abc'); // returns "123 abc abc" |
|
109
|
|
|
* ``` |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $search Search string to look for. |
|
112
|
|
|
* @param string $replace Replacement value for the first found occurrence. |
|
113
|
|
|
* @param string $subject The string you want to look up to replace the first element. |
|
114
|
|
|
* @return mixed Replaced string |
|
115
|
|
|
*/ |
|
116
|
|
|
public static function replaceFirst($search, $replace, $subject) |
|
117
|
|
|
{ |
|
118
|
|
|
return preg_replace('/'.preg_quote($search, '/').'/', $replace, $subject, 1); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Check whether a char or word exists in a string or not. |
|
123
|
|
|
* |
|
124
|
|
|
* This method is case sensitive. The need can be an array with multiple chars or words who |
|
125
|
|
|
* are going to look up in the haystack string. |
|
126
|
|
|
* |
|
127
|
|
|
* If an array of needle words is provided the $strict parameter defines whether all need keys must be found |
|
128
|
|
|
* in the string to get the `true` response or if just one of the keys are found the response is already `true`. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string|array $needle The char or word to find in the $haystack. Can be an array to multi find words or char in the string. |
|
131
|
|
|
* @param string $haystack The haystack where the $needle string should be looked up. |
|
132
|
|
|
* @param boolean $strict If an array of needles is provided the $strict parameter defines whether all keys must be found ($strict = true) or just one result must be found ($strict = false). |
|
133
|
|
|
* @return boolean If an array of values is provided the response may change depending on $findAll. |
|
134
|
|
|
*/ |
|
135
|
|
|
public static function contains($needle, $haystack, $strict = false) |
|
136
|
|
|
{ |
|
137
|
|
|
$needles = (array) $needle; |
|
138
|
|
|
|
|
139
|
|
|
$state = false; |
|
140
|
|
|
|
|
141
|
|
|
foreach ($needles as $item) { |
|
142
|
|
|
$state = (strpos($haystack, $item) !== false); |
|
143
|
|
|
|
|
144
|
|
|
if ($strict && !$state) { |
|
145
|
|
|
return false; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if (!$strict && $state) { |
|
149
|
|
|
return true; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $state; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Minify html by removing spaces, tabs. |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $content |
|
160
|
|
|
* @return mixed |
|
161
|
|
|
* @since 1.0.7 |
|
162
|
|
|
*/ |
|
163
|
|
|
public static function minify($content) |
|
164
|
|
|
{ |
|
165
|
|
|
return preg_replace(['/\>[^\S ]+/s', '/[^\S ]+\</s', '/(\s)+/s'], ['>', '<', '\\1'], trim($content)); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|