1 | <?php |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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 | * ```php |
||
131 | * if (StringHelper::contains('foo', 'the foo bar Bar'')) { |
||
132 | * echo "yes!"; |
||
133 | * } |
||
134 | * ``` |
||
135 | * |
||
136 | * check if one of the given needles exists: |
||
137 | * |
||
138 | * ```php |
||
139 | * if (StringHelper::contains(['jungle', 'hell0], 'Welcome to the jungle!)) { |
||
140 | * echo "yes!"; |
||
141 | * } |
||
142 | * ``` |
||
143 | * |
||
144 | * @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. |
||
145 | * @param string $haystack The haystack where the $needle string should be looked up. A string or phrase with words. |
||
146 | * @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). |
||
147 | * @return boolean If an array of values is provided the response may change depending on $findAll. |
||
148 | */ |
||
149 | public static function contains($needle, $haystack, $strict = false) |
||
169 | |||
170 | /** |
||
171 | * "Minify" html content. |
||
172 | * |
||
173 | * + remove space |
||
174 | * + remove tabs |
||
175 | * + remove newlines |
||
176 | * + remove html comments |
||
177 | * |
||
178 | * @param string $content The content to minify. |
||
179 | * @param array $options Optional arguments to provide for minification: |
||
180 | * - comments: boolean, where html comments should be removed or not. defaults to false |
||
181 | * @return mixed Returns the minified content. |
||
182 | * @since 1.0.7 |
||
183 | */ |
||
184 | public static function minify($content, array $options = []) |
||
195 | |||
196 | /** |
||
197 | * Cut the given word/string from the content. Its truncates to the left side and to the right side of the word. |
||
198 | * |
||
199 | * An example of how a sentenced is cut: |
||
200 | * |
||
201 | * ```php |
||
202 | * $cut = StringHelper::truncateMiddle('the quick fox jumped over the lazy dog', 'jumped', 12); |
||
203 | * echo $cut; // ..e quick fox jumped over the la.. |
||
204 | * ``` |
||
205 | * |
||
206 | * @param string $content The content to cut the words from. |
||
207 | * @param string $word The word which should be in the middle of the string |
||
208 | * @param integer $length The amount of the chars to cut on the left and right side from the word. |
||
209 | * @param string $affix The chars which should be used for prefix and suffix when string is cuted. |
||
210 | * @param boolean $caseSensitive Whether the search word in the string even when lower/upper case is not correct. |
||
211 | * @since 1.0.12 |
||
212 | */ |
||
213 | public static function truncateMiddle($content, $word, $length, $affix = '..', $caseSensitive = false) |
||
238 | |||
239 | /** |
||
240 | * Highlight a word within a content. |
||
241 | * |
||
242 | * Since version 1.0.14 an array of words to highlight is possible. |
||
243 | * |
||
244 | * > This function IS NOT case sensitive! |
||
245 | * |
||
246 | * |
||
247 | * |
||
248 | * @param string $content The content to find the word. |
||
249 | * @param string $word The word to find within the content. |
||
250 | * @param string $markup The markup used wrap the word to highlight. |
||
251 | * @since 1.0.12 |
||
252 | */ |
||
253 | public static function highlightWord($content, $word, $markup = '<b>%s</b>') |
||
274 | |||
275 | /** |
||
276 | * Multibyte-safe str_split funciton. |
||
277 | * |
||
278 | * @param string $string The string to split into an array |
||
279 | * @param integer $length The length of the chars to cut. |
||
280 | * @since 1.0.12 |
||
281 | */ |
||
282 | public static function mb_str_split($string, $length = 1) |
||
293 | |||
294 | /** |
||
295 | * Check whether a value is numeric or not. |
||
296 | * |
||
297 | * There are situations where is_numeric does not provide the desried result, |
||
298 | * like for example `is_numeric('3e30')` would return true, as e can be considered |
||
299 | * as exponential operator. |
||
300 | * |
||
301 | * Therfore this function checks with regex whether values or 0-9 if strict is enabled, |
||
302 | * which is default behavior. |
||
303 | * |
||
304 | * @param mixed $value The value to check. |
||
305 | * @param boolean $strict |
||
306 | * @return boolean |
||
307 | */ |
||
308 | public static function isNummeric($value, $strict = true) |
||
324 | } |
||
325 |