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 | * @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) |
||
155 | |||
156 | /** |
||
157 | * "Minify" html content. |
||
158 | * |
||
159 | * + remove space |
||
160 | * + remove tabs |
||
161 | * + remove newlines |
||
162 | * + remove html comments |
||
163 | * |
||
164 | * @param string $content The content to minify. |
||
165 | * @param array $options Optional arguments to provide for minification: |
||
166 | * - comments: boolean, where html comments should be removed or not. defaults to false |
||
167 | * @return mixed Returns the minified content. |
||
168 | * @since 1.0.7 |
||
169 | */ |
||
170 | public static function minify($content, array $options = []) |
||
181 | |||
182 | /** |
||
183 | * Cut the given word/string from the content. Its truncates to the left side and to the right side of the word. |
||
184 | * |
||
185 | * An example of how a sentenced is cut: |
||
186 | * |
||
187 | * ```php |
||
188 | * $cut = StringHelper::truncateMiddle('the quick fox jumped over the lazy dog', 'jumped', 12); |
||
189 | * echo $cut; // ..e quick fox jumped over the la.. |
||
190 | * ``` |
||
191 | * |
||
192 | * @param string $content The content to cut the words from. |
||
193 | * @param string $word The word which should be in the middle of the string |
||
194 | * @param integer $length The amount of the chars to cut on the left and right side from the word. |
||
195 | * @param string $affix The chars which should be used for prefix and suffix when string is cuted. |
||
196 | * @param boolean $caseSensitive Whether the search word in the string even when lower/upper case is not correct. |
||
197 | * @since 1.0.12 |
||
198 | */ |
||
199 | public static function truncateMiddle($content, $word, $length, $affix = '..', $caseSensitive = false) |
||
224 | |||
225 | /** |
||
226 | * Highlight a word within a content. |
||
227 | * |
||
228 | * Since version 1.0.14 an array of words to highlight is possible. |
||
229 | * |
||
230 | * > This function IS NOT case sensitive! |
||
231 | * |
||
232 | * |
||
233 | * |
||
234 | * @param string $content The content to find the word. |
||
235 | * @param string $word The word to find within the content. |
||
236 | * @param string $markup The markup used wrap the word to highlight. |
||
237 | * @since 1.0.12 |
||
238 | */ |
||
239 | public static function highlightWord($content, $word, $markup = '<b>%s</b>') |
||
260 | |||
261 | /** |
||
262 | * Multibyte-safe str_split funciton. |
||
263 | * |
||
264 | * @param string $string The string to split into an array |
||
265 | * @param integer $length The length of the chars to cut. |
||
266 | * @since 1.0.12 |
||
267 | */ |
||
268 | public static function mb_str_split($string, $length = 1) |
||
279 | } |
||
280 |