1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license https://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\helpers; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* BaseStringHelper provides concrete implementation for [[StringHelper]]. |
14
|
|
|
* |
15
|
|
|
* Do not use BaseStringHelper. Use [[StringHelper]] instead. |
16
|
|
|
* |
17
|
|
|
* @author Qiang Xue <[email protected]> |
18
|
|
|
* @author Alex Makarov <[email protected]> |
19
|
|
|
* @since 2.0 |
20
|
|
|
*/ |
21
|
|
|
class BaseStringHelper |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Returns the number of bytes in the given string. |
25
|
|
|
* This method ensures the string is treated as a byte array by using `mb_strlen()`. |
26
|
|
|
* |
27
|
|
|
* @param string $string the string being measured for length |
28
|
|
|
* @return int the number of bytes in the given string. |
29
|
|
|
*/ |
30
|
424 |
|
public static function byteLength($string) |
31
|
|
|
{ |
32
|
424 |
|
return mb_strlen((string)$string, '8bit'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns the portion of string specified by the start and length parameters. |
37
|
|
|
* This method ensures the string is treated as a byte array by using `mb_substr()`. |
38
|
|
|
* |
39
|
|
|
* @param string $string the input string. Must be one character or longer. |
40
|
|
|
* @param int $start the starting position |
41
|
|
|
* @param int|null $length the desired portion length. If not specified or `null`, there will be |
42
|
|
|
* no limit on length i.e. the output will be until the end of the string. |
43
|
|
|
* @return string the extracted part of string, or FALSE on failure or an empty string. |
44
|
|
|
* @see https://www.php.net/manual/en/function.substr.php |
45
|
|
|
*/ |
46
|
154 |
|
public static function byteSubstr($string, $start, $length = null) |
47
|
|
|
{ |
48
|
154 |
|
if ($length === null) { |
49
|
47 |
|
$length = static::byteLength($string); |
50
|
|
|
} |
51
|
|
|
|
52
|
154 |
|
return mb_substr((string)$string, $start, $length, '8bit'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns the trailing name component of a path. |
57
|
|
|
* This method is similar to the php function `basename()` except that it will |
58
|
|
|
* treat both \ and / as directory separators, independent of the operating system. |
59
|
|
|
* This method was mainly created to work on php namespaces. When working with real |
60
|
|
|
* file paths, php's `basename()` should work fine for you. |
61
|
|
|
* Note: this method is not aware of the actual filesystem, or path components such as "..". |
62
|
|
|
* |
63
|
|
|
* @param string $path A path string. |
64
|
|
|
* @param string $suffix If the name component ends in suffix this will also be cut off. |
65
|
|
|
* @return string the trailing name component of the given path. |
66
|
|
|
* @see https://www.php.net/manual/en/function.basename.php |
67
|
|
|
*/ |
68
|
23 |
|
public static function basename($path, $suffix = '') |
69
|
|
|
{ |
70
|
23 |
|
$path = (string)$path; |
71
|
|
|
|
72
|
23 |
|
$len = mb_strlen($suffix); |
73
|
23 |
|
if ($len > 0 && mb_substr($path, -$len) === $suffix) { |
74
|
1 |
|
$path = mb_substr($path, 0, -$len); |
75
|
|
|
} |
76
|
|
|
|
77
|
23 |
|
$path = rtrim(str_replace('\\', '/', $path), '/'); |
78
|
23 |
|
$pos = mb_strrpos($path, '/'); |
79
|
23 |
|
if ($pos !== false) { |
80
|
23 |
|
return mb_substr($path, $pos + 1); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return $path; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns parent directory's path. |
88
|
|
|
* This method is similar to `dirname()` except that it will treat |
89
|
|
|
* both \ and / as directory separators, independent of the operating system. |
90
|
|
|
* |
91
|
|
|
* @param string $path A path string. |
92
|
|
|
* @return string the parent directory's path. |
93
|
|
|
* @see https://www.php.net/manual/en/function.basename.php |
94
|
|
|
*/ |
95
|
11 |
|
public static function dirname($path) |
96
|
|
|
{ |
97
|
11 |
|
$normalizedPath = rtrim( |
98
|
11 |
|
str_replace('\\', '/', (string)$path), |
99
|
11 |
|
'/' |
100
|
11 |
|
); |
101
|
11 |
|
$separatorPosition = mb_strrpos($normalizedPath, '/'); |
102
|
|
|
|
103
|
11 |
|
if ($separatorPosition !== false) { |
104
|
9 |
|
return mb_substr($path, 0, $separatorPosition); |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
return ''; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Truncates a string to the number of characters specified. |
112
|
|
|
* |
113
|
|
|
* In order to truncate for an exact length, the $suffix char length must be counted towards the $length. For example |
114
|
|
|
* to have a string which is exactly 255 long with $suffix `...` of 3 chars, then `StringHelper::truncate($string, 252, '...')` |
115
|
|
|
* must be used to ensure you have 255 long string afterwards. |
116
|
|
|
* |
117
|
|
|
* @param string $string The string to truncate. |
118
|
|
|
* @param int $length How many characters from original string to include into truncated string. |
119
|
|
|
* @param string $suffix String to append to the end of truncated string. |
120
|
|
|
* @param string|null $encoding The charset to use, defaults to charset currently used by application. |
121
|
|
|
* @param bool $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags. |
122
|
|
|
* This parameter is available since version 2.0.1. |
123
|
|
|
* @return string the truncated string. |
124
|
|
|
*/ |
125
|
1 |
|
public static function truncate($string, $length, $suffix = '...', $encoding = null, $asHtml = false) |
126
|
|
|
{ |
127
|
1 |
|
$string = (string)$string; |
128
|
|
|
|
129
|
1 |
|
if ($encoding === null) { |
130
|
1 |
|
$encoding = Yii::$app ? Yii::$app->charset : 'UTF-8'; |
131
|
|
|
} |
132
|
1 |
|
if ($asHtml) { |
133
|
1 |
|
return static::truncateHtml($string, $length, $suffix, $encoding); |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
if (mb_strlen($string, $encoding) > $length) { |
137
|
1 |
|
return rtrim(mb_substr($string, 0, $length, $encoding)) . $suffix; |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
return $string; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Truncates a string to the number of words specified. |
145
|
|
|
* |
146
|
|
|
* @param string $string The string to truncate. |
147
|
|
|
* @param int $count How many words from original string to include into truncated string. |
148
|
|
|
* @param string $suffix String to append to the end of truncated string. |
149
|
|
|
* @param bool $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags. |
150
|
|
|
* This parameter is available since version 2.0.1. |
151
|
|
|
* @return string the truncated string. |
152
|
|
|
*/ |
153
|
1 |
|
public static function truncateWords($string, $count, $suffix = '...', $asHtml = false) |
154
|
|
|
{ |
155
|
1 |
|
if ($asHtml) { |
156
|
1 |
|
return static::truncateHtml($string, $count, $suffix); |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
$words = preg_split('/(\s+)/u', trim($string), 0, PREG_SPLIT_DELIM_CAPTURE); |
160
|
1 |
|
if (count($words) / 2 > $count) { |
161
|
1 |
|
return implode('', array_slice($words, 0, ($count * 2) - 1)) . $suffix; |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
return $string; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Truncate a string while preserving the HTML. |
169
|
|
|
* |
170
|
|
|
* @param string $string The string to truncate |
171
|
|
|
* @param int $count The counter |
172
|
|
|
* @param string $suffix String to append to the end of the truncated string. |
173
|
|
|
* @param string|bool $encoding Encoding flag or charset. |
174
|
|
|
* @return string |
175
|
|
|
* @since 2.0.1 |
176
|
|
|
*/ |
177
|
2 |
|
protected static function truncateHtml($string, $count, $suffix, $encoding = false) |
178
|
|
|
{ |
179
|
2 |
|
$config = \HTMLPurifier_Config::create(null); |
180
|
2 |
|
if (Yii::$app !== null) { |
181
|
|
|
$config->set('Cache.SerializerPath', Yii::$app->getRuntimePath()); |
182
|
|
|
} |
183
|
2 |
|
$lexer = \HTMLPurifier_Lexer::create($config); |
184
|
2 |
|
$tokens = $lexer->tokenizeHTML($string, $config, new \HTMLPurifier_Context()); |
185
|
2 |
|
$openTokens = []; |
186
|
2 |
|
$totalCount = 0; |
187
|
2 |
|
$depth = 0; |
188
|
2 |
|
$truncated = []; |
189
|
2 |
|
foreach ($tokens as $token) { |
190
|
2 |
|
if ($token instanceof \HTMLPurifier_Token_Start) { //Tag begins |
191
|
2 |
|
$openTokens[$depth] = $token->name; |
192
|
2 |
|
$truncated[] = $token; |
193
|
2 |
|
++$depth; |
194
|
2 |
|
} elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) { //Text |
195
|
2 |
|
if (false === $encoding) { |
196
|
1 |
|
preg_match('/^(\s*)/um', $token->data, $prefixSpace) ?: $prefixSpace = ['', '']; |
197
|
1 |
|
$token->data = $prefixSpace[1] . self::truncateWords(ltrim($token->data), $count - $totalCount, ''); |
198
|
1 |
|
$currentCount = self::countWords($token->data); |
199
|
|
|
} else { |
200
|
1 |
|
$token->data = self::truncate($token->data, $count - $totalCount, '', $encoding); |
|
|
|
|
201
|
1 |
|
$currentCount = mb_strlen($token->data, $encoding); |
|
|
|
|
202
|
|
|
} |
203
|
2 |
|
$totalCount += $currentCount; |
204
|
2 |
|
$truncated[] = $token; |
205
|
2 |
|
} elseif ($token instanceof \HTMLPurifier_Token_End) { //Tag ends |
206
|
2 |
|
if ($token->name === $openTokens[$depth - 1]) { |
207
|
2 |
|
--$depth; |
208
|
2 |
|
unset($openTokens[$depth]); |
209
|
2 |
|
$truncated[] = $token; |
210
|
|
|
} |
211
|
2 |
|
} elseif ($token instanceof \HTMLPurifier_Token_Empty) { //Self contained tags, i.e. <img/> etc. |
212
|
2 |
|
$truncated[] = $token; |
213
|
|
|
} |
214
|
2 |
|
if ($totalCount >= $count) { |
215
|
2 |
|
if (0 < count($openTokens)) { |
216
|
2 |
|
krsort($openTokens); |
217
|
2 |
|
foreach ($openTokens as $name) { |
218
|
2 |
|
$truncated[] = new \HTMLPurifier_Token_End($name); |
219
|
|
|
} |
220
|
|
|
} |
221
|
2 |
|
break; |
222
|
|
|
} |
223
|
|
|
} |
224
|
2 |
|
$context = new \HTMLPurifier_Context(); |
225
|
2 |
|
$generator = new \HTMLPurifier_Generator($config, $context); |
226
|
2 |
|
return $generator->generateFromTokens($truncated) . ($totalCount >= $count ? $suffix : ''); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param string $string The input string. |
231
|
|
|
* @param string $needle Part to search inside $string |
232
|
|
|
* @param bool $caseSensitive Case sensitive search. Default is true. When case sensitive is enabled, `$with` must |
233
|
|
|
* exactly match the starting of the string in order to get a true value. |
234
|
|
|
* @return bool |
235
|
|
|
*/ |
236
|
79 |
|
public static function contains($string, $needle, $caseSensitive = true) |
237
|
|
|
{ |
238
|
79 |
|
$string = (string)$string; |
239
|
79 |
|
$needle = (string)$needle; |
240
|
|
|
|
241
|
79 |
|
if ($caseSensitive) { |
242
|
79 |
|
if (function_exists('str_contains')) { |
243
|
79 |
|
return str_contains($string, $needle); |
244
|
|
|
} |
245
|
|
|
$encoding = Yii::$app ? Yii::$app->charset : 'UTF-8'; |
246
|
|
|
return mb_strpos($string, $needle, 0, $encoding) !== false; |
247
|
|
|
} |
248
|
1 |
|
$encoding = Yii::$app ? Yii::$app->charset : 'UTF-8'; |
249
|
1 |
|
return mb_stripos($string, $needle, 0, $encoding) !== false; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Check if given string starts with specified substring. Binary and multibyte safe. |
254
|
|
|
* |
255
|
|
|
* @param string $string Input string |
256
|
|
|
* @param string $with Part to search inside the $string |
257
|
|
|
* @param bool $caseSensitive Case sensitive search. Default is true. When case sensitive is enabled, `$with` must |
258
|
|
|
* exactly match the starting of the string in order to get a true value. |
259
|
|
|
* @return bool Returns true if first input starts with second input, false otherwise |
260
|
|
|
*/ |
261
|
20 |
|
public static function startsWith($string, $with, $caseSensitive = true) |
262
|
|
|
{ |
263
|
20 |
|
$string = (string)$string; |
264
|
20 |
|
$with = (string)$with; |
265
|
|
|
|
266
|
20 |
|
if (!$bytes = static::byteLength($with)) { |
267
|
3 |
|
return true; |
268
|
|
|
} |
269
|
17 |
|
if ($caseSensitive) { |
270
|
16 |
|
return strncmp($string, $with, $bytes) === 0; |
271
|
|
|
} |
272
|
|
|
|
273
|
15 |
|
$encoding = Yii::$app ? Yii::$app->charset : 'UTF-8'; |
274
|
15 |
|
$string = static::byteSubstr($string, 0, $bytes); |
275
|
|
|
|
276
|
15 |
|
return mb_strtolower($string, $encoding) === mb_strtolower($with, $encoding); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Check if given string ends with specified substring. Binary and multibyte safe. |
281
|
|
|
* |
282
|
|
|
* @param string $string Input string to check |
283
|
|
|
* @param string $with Part to search inside of the `$string`. |
284
|
|
|
* @param bool $caseSensitive Case sensitive search. Default is true. When case sensitive is enabled, `$with` must |
285
|
|
|
* exactly match the ending of the string in order to get a true value. |
286
|
|
|
* @return bool Returns true if first input ends with second input, false otherwise |
287
|
|
|
*/ |
288
|
30 |
|
public static function endsWith($string, $with, $caseSensitive = true) |
289
|
|
|
{ |
290
|
30 |
|
$string = (string)$string; |
291
|
30 |
|
$with = (string)$with; |
292
|
|
|
|
293
|
30 |
|
if (!$bytes = static::byteLength($with)) { |
294
|
3 |
|
return true; |
295
|
|
|
} |
296
|
27 |
|
if ($caseSensitive) { |
297
|
|
|
// Warning check, see https://php.net/substr-compare#refsect1-function.substr-compare-returnvalues |
298
|
16 |
|
if (static::byteLength($string) < $bytes) { |
299
|
3 |
|
return false; |
300
|
|
|
} |
301
|
|
|
|
302
|
13 |
|
return substr_compare($string, $with, -$bytes, $bytes) === 0; |
303
|
|
|
} |
304
|
|
|
|
305
|
25 |
|
$encoding = Yii::$app ? Yii::$app->charset : 'UTF-8'; |
306
|
25 |
|
$string = static::byteSubstr($string, -$bytes); |
307
|
|
|
|
308
|
25 |
|
return mb_strtolower($string, $encoding) === mb_strtolower($with, $encoding); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Explodes string into array, optionally trims values and skips empty ones. |
313
|
|
|
* |
314
|
|
|
* @param string $string String to be exploded. |
315
|
|
|
* @param string $delimiter Delimiter. Default is ','. |
316
|
|
|
* @param mixed $trim Whether to trim each element. Can be: |
317
|
|
|
* - boolean - to trim normally; |
318
|
|
|
* - string - custom characters to trim. Will be passed as a second argument to `trim()` function. |
319
|
|
|
* - callable - will be called for each value instead of trim. Takes the only argument - value. |
320
|
|
|
* @param bool $skipEmpty Whether to skip empty strings between delimiters. Default is false. |
321
|
|
|
* @return array |
322
|
|
|
* @since 2.0.4 |
323
|
|
|
*/ |
324
|
1 |
|
public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false) |
325
|
|
|
{ |
326
|
1 |
|
$result = explode($delimiter, $string); |
327
|
1 |
|
if ($trim !== false) { |
328
|
1 |
|
if ($trim === true) { |
329
|
1 |
|
$trim = 'trim'; |
330
|
1 |
|
} elseif (!is_callable($trim)) { |
331
|
1 |
|
$trim = function ($v) use ($trim) { |
332
|
1 |
|
return trim($v, $trim); |
333
|
1 |
|
}; |
334
|
|
|
} |
335
|
1 |
|
$result = array_map($trim, $result); |
336
|
|
|
} |
337
|
1 |
|
if ($skipEmpty) { |
338
|
|
|
// Wrapped with array_values to make array keys sequential after empty values removing |
339
|
1 |
|
$result = array_values( |
340
|
1 |
|
array_filter( |
341
|
1 |
|
$result, |
342
|
1 |
|
function ($value) { |
343
|
1 |
|
return $value !== ''; |
344
|
1 |
|
} |
345
|
1 |
|
) |
346
|
1 |
|
); |
347
|
|
|
} |
348
|
|
|
|
349
|
1 |
|
return $result; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Counts words in a string. |
354
|
|
|
* |
355
|
|
|
* @param string $string the text to calculate |
356
|
|
|
* @return int |
357
|
|
|
* @since 2.0.8 |
358
|
|
|
*/ |
359
|
2 |
|
public static function countWords($string) |
360
|
|
|
{ |
361
|
2 |
|
return count(preg_split('/\s+/u', $string, 0, PREG_SPLIT_NO_EMPTY)); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Returns string representation of number value with replaced commas to dots, if decimal point |
366
|
|
|
* of current locale is comma. |
367
|
|
|
* |
368
|
|
|
* @param int|float|string $value the value to normalize. |
369
|
|
|
* @return string |
370
|
|
|
* @since 2.0.11 |
371
|
|
|
*/ |
372
|
34 |
|
public static function normalizeNumber($value) |
373
|
|
|
{ |
374
|
34 |
|
$value = (string)$value; |
375
|
|
|
|
376
|
34 |
|
$localeInfo = localeconv(); |
377
|
34 |
|
$decimalSeparator = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : null; |
378
|
|
|
|
379
|
34 |
|
if ($decimalSeparator !== null && $decimalSeparator !== '.') { |
380
|
4 |
|
$value = str_replace($decimalSeparator, '.', $value); |
381
|
|
|
} |
382
|
|
|
|
383
|
34 |
|
return $value; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Encodes string into "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648). |
388
|
|
|
* |
389
|
|
|
* > Note: Base 64 padding `=` may be at the end of the returned string. |
390
|
|
|
* > `=` is not transparent to URL encoding. |
391
|
|
|
* |
392
|
|
|
* @param string $input the string to encode. |
393
|
|
|
* @return string encoded string. |
394
|
|
|
* @see https://tools.ietf.org/html/rfc4648#page-7 |
395
|
|
|
* @since 2.0.12 |
396
|
|
|
*/ |
397
|
112 |
|
public static function base64UrlEncode($input) |
398
|
|
|
{ |
399
|
112 |
|
return strtr(base64_encode($input), '+/', '-_'); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Decodes "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648). |
404
|
|
|
* |
405
|
|
|
* @param string $input encoded string. |
406
|
|
|
* @return string decoded string. |
407
|
|
|
* @see https://tools.ietf.org/html/rfc4648#page-7 |
408
|
|
|
* @since 2.0.12 |
409
|
|
|
*/ |
410
|
13 |
|
public static function base64UrlDecode($input) |
411
|
|
|
{ |
412
|
13 |
|
return base64_decode(strtr($input, '-_', '+/')); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* Safely casts a float to string independent of the current locale. |
417
|
|
|
* The decimal separator will always be `.`. |
418
|
|
|
* |
419
|
|
|
* @param float|int $number a floating point number or integer. |
420
|
|
|
* @return string the string representation of the number. |
421
|
|
|
* @since 2.0.13 |
422
|
|
|
*/ |
423
|
10 |
|
public static function floatToString($number) |
424
|
|
|
{ |
425
|
|
|
// . and , are the only decimal separators known in ICU data, |
426
|
|
|
// so its safe to call str_replace here |
427
|
10 |
|
return str_replace(',', '.', (string)$number); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Checks if the passed string would match the given shell wildcard pattern. |
432
|
|
|
* This function emulates [[fnmatch()]], which may be unavailable at certain environment, using PCRE. |
433
|
|
|
* |
434
|
|
|
* @param string $pattern the shell wildcard pattern. |
435
|
|
|
* @param string $string the tested string. |
436
|
|
|
* @param array $options options for matching. Valid options are: |
437
|
|
|
* |
438
|
|
|
* - caseSensitive: bool, whether pattern should be case sensitive. Defaults to `true`. |
439
|
|
|
* - escape: bool, whether backslash escaping is enabled. Defaults to `true`. |
440
|
|
|
* - filePath: bool, whether slashes in string only matches slashes in the given pattern. Defaults to `false`. |
441
|
|
|
* |
442
|
|
|
* @return bool whether the string matches pattern or not. |
443
|
|
|
* @since 2.0.14 |
444
|
|
|
*/ |
445
|
244 |
|
public static function matchWildcard($pattern, $string, $options = []) |
446
|
|
|
{ |
447
|
244 |
|
if ($pattern === '*' && empty($options['filePath'])) { |
448
|
5 |
|
return true; |
449
|
|
|
} |
450
|
|
|
|
451
|
240 |
|
$replacements = [ |
452
|
240 |
|
'\\\\\\\\' => '\\\\', |
453
|
240 |
|
'\\\\\\*' => '[*]', |
454
|
240 |
|
'\\\\\\?' => '[?]', |
455
|
240 |
|
'\*' => '.*', |
456
|
240 |
|
'\?' => '.', |
457
|
240 |
|
'\[\!' => '[^', |
458
|
240 |
|
'\[' => '[', |
459
|
240 |
|
'\]' => ']', |
460
|
240 |
|
'\-' => '-', |
461
|
240 |
|
]; |
462
|
|
|
|
463
|
240 |
|
if (isset($options['escape']) && !$options['escape']) { |
464
|
9 |
|
unset($replacements['\\\\\\\\']); |
465
|
9 |
|
unset($replacements['\\\\\\*']); |
466
|
9 |
|
unset($replacements['\\\\\\?']); |
467
|
|
|
} |
468
|
|
|
|
469
|
240 |
|
if (!empty($options['filePath'])) { |
470
|
12 |
|
$replacements['\*'] = '[^/\\\\]*'; |
471
|
12 |
|
$replacements['\?'] = '[^/\\\\]'; |
472
|
|
|
} |
473
|
|
|
|
474
|
240 |
|
$pattern = strtr(preg_quote($pattern, '#'), $replacements); |
475
|
240 |
|
$pattern = '#^' . $pattern . '$#us'; |
476
|
|
|
|
477
|
240 |
|
if (isset($options['caseSensitive']) && !$options['caseSensitive']) { |
478
|
2 |
|
$pattern .= 'i'; |
479
|
|
|
} |
480
|
|
|
|
481
|
240 |
|
return preg_match($pattern, (string)$string) === 1; |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* This method provides a unicode-safe implementation of built-in PHP function `ucfirst()`. |
486
|
|
|
* |
487
|
|
|
* @param string $string the string to be proceeded |
488
|
|
|
* @param string $encoding Optional, defaults to "UTF-8" |
489
|
|
|
* @return string |
490
|
|
|
* @see https://www.php.net/manual/en/function.ucfirst.php |
491
|
|
|
* @since 2.0.16 |
492
|
|
|
* @phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
493
|
|
|
*/ |
494
|
229 |
|
public static function mb_ucfirst($string, $encoding = 'UTF-8') |
495
|
|
|
{ |
496
|
229 |
|
$firstChar = mb_substr((string)$string, 0, 1, $encoding); |
497
|
229 |
|
$rest = mb_substr((string)$string, 1, null, $encoding); |
498
|
|
|
|
499
|
229 |
|
return mb_strtoupper($firstChar, $encoding) . $rest; |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* This method provides a unicode-safe implementation of built-in PHP function `ucwords()`. |
504
|
|
|
* |
505
|
|
|
* @param string $string the string to be proceeded |
506
|
|
|
* @param string $encoding Optional, defaults to "UTF-8" |
507
|
|
|
* @return string |
508
|
|
|
* @see https://www.php.net/manual/en/function.ucwords |
509
|
|
|
* @since 2.0.16 |
510
|
|
|
* @phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
511
|
|
|
*/ |
512
|
226 |
|
public static function mb_ucwords($string, $encoding = 'UTF-8') |
513
|
|
|
{ |
514
|
226 |
|
$string = (string)$string; |
515
|
226 |
|
if (empty($string)) { |
516
|
3 |
|
return $string; |
517
|
|
|
} |
518
|
|
|
|
519
|
223 |
|
$parts = preg_split('/(\s+\W+\s+|^\W+\s+|\s+)/u', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); |
520
|
223 |
|
$ucfirstEven = trim(mb_substr($parts[0], -1, 1, $encoding)) === ''; |
521
|
223 |
|
foreach ($parts as $key => $value) { |
522
|
223 |
|
$isEven = (bool)($key % 2); |
523
|
223 |
|
if ($ucfirstEven === $isEven) { |
524
|
223 |
|
$parts[$key] = static::mb_ucfirst($value, $encoding); |
525
|
|
|
} |
526
|
|
|
} |
527
|
|
|
|
528
|
223 |
|
return implode('', $parts); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* Masks a portion of a string with a repeated character. |
533
|
|
|
* This method is multibyte-safe. |
534
|
|
|
* |
535
|
|
|
* @param string $string The input string. |
536
|
|
|
* @param int $start The starting position from where to begin masking. |
537
|
|
|
* This can be a positive or negative integer. |
538
|
|
|
* Positive values count from the beginning, |
539
|
|
|
* negative values count from the end of the string. |
540
|
|
|
* @param int $length The length of the section to be masked. |
541
|
|
|
* The masking will start from the $start position |
542
|
|
|
* and continue for $length characters. |
543
|
|
|
* @param string $mask The character to use for masking. The default is '*'. |
544
|
|
|
* @return string The masked string. |
545
|
|
|
*/ |
546
|
1 |
|
public static function mask($string, $start, $length, $mask = '*') |
547
|
|
|
{ |
548
|
1 |
|
$strLength = mb_strlen($string, 'UTF-8'); |
549
|
|
|
|
550
|
|
|
// Return original string if start position is out of bounds |
551
|
1 |
|
if ($start >= $strLength || $start < -$strLength) { |
552
|
1 |
|
return $string; |
553
|
|
|
} |
554
|
|
|
|
555
|
1 |
|
$masked = mb_substr($string, 0, $start, 'UTF-8'); |
556
|
1 |
|
$masked .= str_repeat($mask, abs($length)); |
|
|
|
|
557
|
1 |
|
$masked .= mb_substr($string, $start + abs($length), null, 'UTF-8'); |
|
|
|
|
558
|
|
|
|
559
|
1 |
|
return $masked; |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
/** |
563
|
|
|
* Returns the portion of the string that lies between the first occurrence of the start string |
564
|
|
|
* and the last occurrence of the end string after that. |
565
|
|
|
* |
566
|
|
|
* @param string $string The input string. |
567
|
|
|
* @param string $start The string marking the start of the portion to extract. |
568
|
|
|
* @param string $end The string marking the end of the portion to extract. |
569
|
|
|
* @return string|null The portion of the string between the first occurrence of |
570
|
|
|
* start and the last occurrence of end, or null if either start or end cannot be found. |
571
|
|
|
*/ |
572
|
12 |
|
public static function findBetween($string, $start, $end) |
573
|
|
|
{ |
574
|
12 |
|
$startPos = mb_strpos($string, $start); |
575
|
|
|
|
576
|
12 |
|
if ($startPos === false) { |
577
|
3 |
|
return null; |
578
|
|
|
} |
579
|
|
|
|
580
|
9 |
|
$startPos += mb_strlen($start); |
581
|
9 |
|
$endPos = mb_strrpos($string, $end, $startPos); |
582
|
|
|
|
583
|
9 |
|
if ($endPos === false) { |
584
|
2 |
|
return null; |
585
|
|
|
} |
586
|
|
|
|
587
|
7 |
|
return mb_substr($string, $startPos, $endPos - $startPos); |
588
|
|
|
} |
589
|
|
|
} |
590
|
|
|
|