Passed
Pull Request — master (#20269)
by
unknown
14:48 queued 06:12
created

BaseStringHelper::contains()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 9
c 1
b 1
f 0
nc 5
nop 3
dl 0
loc 15
ccs 8
cts 10
cp 0.8
crap 5.2
rs 9.6111
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);
0 ignored issues
show
Bug introduced by
It seems like $encoding can also be of type true; however, parameter $encoding of yii\helpers\BaseStringHelper::truncate() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

200
                    $token->data = self::truncate($token->data, $count - $totalCount, '', /** @scrutinizer ignore-type */ $encoding);
Loading history...
201 1
                    $currentCount = mb_strlen($token->data, $encoding);
0 ignored issues
show
Bug introduced by
It seems like $encoding can also be of type true; however, parameter $encoding of mb_strlen() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

201
                    $currentCount = mb_strlen($token->data, /** @scrutinizer ignore-type */ $encoding);
Loading history...
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
            // can be replaced with just the str_contains call when minimum supported PHP version is raised to 8.0 or higher
243 79
            if (function_exists('str_contains')) {
244 79
                return str_contains($string, $needle);
245
            }
246
            $encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
247
            return mb_strpos($string, $needle, 0, $encoding) !== false;
248
        }
249 1
        $encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
250 1
        return mb_stripos($string, $needle, 0, $encoding) !== false;
251
    }
252
253
    /**
254
     * Check if given string starts with specified substring. Binary and multibyte safe.
255
     *
256
     * @param string $string Input string
257
     * @param string $with Part to search inside the $string
258
     * @param bool $caseSensitive Case sensitive search. Default is true. When case sensitive is enabled, `$with` must
259
     * exactly match the starting of the string in order to get a true value.
260
     * @return bool Returns true if first input starts with second input, false otherwise
261
     */
262 20
    public static function startsWith($string, $with, $caseSensitive = true)
263
    {
264 20
        $string = (string)$string;
265 20
        $with = (string)$with;
266
267 20
        if (!$bytes = static::byteLength($with)) {
268 3
            return true;
269
        }
270 17
        if ($caseSensitive) {
271 16
            return strncmp($string, $with, $bytes) === 0;
272
        }
273
274 15
        $encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
275 15
        $string = static::byteSubstr($string, 0, $bytes);
276
277 15
        return mb_strtolower($string, $encoding) === mb_strtolower($with, $encoding);
278
    }
279
280
    /**
281
     * Check if given string ends with specified substring. Binary and multibyte safe.
282
     *
283
     * @param string $string Input string to check
284
     * @param string $with Part to search inside of the `$string`.
285
     * @param bool $caseSensitive Case sensitive search. Default is true. When case sensitive is enabled, `$with` must
286
     * exactly match the ending of the string in order to get a true value.
287
     * @return bool Returns true if first input ends with second input, false otherwise
288
     */
289 30
    public static function endsWith($string, $with, $caseSensitive = true)
290
    {
291 30
        $string = (string)$string;
292 30
        $with = (string)$with;
293
294 30
        if (!$bytes = static::byteLength($with)) {
295 3
            return true;
296
        }
297 27
        if ($caseSensitive) {
298
            // Warning check, see https://php.net/substr-compare#refsect1-function.substr-compare-returnvalues
299 16
            if (static::byteLength($string) < $bytes) {
300 3
                return false;
301
            }
302
303 13
            return substr_compare($string, $with, -$bytes, $bytes) === 0;
304
        }
305
306 25
        $encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
307 25
        $string = static::byteSubstr($string, -$bytes);
308
309 25
        return mb_strtolower($string, $encoding) === mb_strtolower($with, $encoding);
310
    }
311
312
    /**
313
     * Explodes string into array, optionally trims values and skips empty ones.
314
     *
315
     * @param string $string String to be exploded.
316
     * @param string $delimiter Delimiter. Default is ','.
317
     * @param mixed $trim Whether to trim each element. Can be:
318
     *   - boolean - to trim normally;
319
     *   - string - custom characters to trim. Will be passed as a second argument to `trim()` function.
320
     *   - callable - will be called for each value instead of trim. Takes the only argument - value.
321
     * @param bool $skipEmpty Whether to skip empty strings between delimiters. Default is false.
322
     * @return array
323
     * @since 2.0.4
324
     */
325 1
    public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false)
326
    {
327 1
        $result = explode($delimiter, $string);
328 1
        if ($trim !== false) {
329 1
            if ($trim === true) {
330 1
                $trim = 'trim';
331 1
            } elseif (!is_callable($trim)) {
332 1
                $trim = function ($v) use ($trim) {
333 1
                    return trim($v, $trim);
334 1
                };
335
            }
336 1
            $result = array_map($trim, $result);
337
        }
338 1
        if ($skipEmpty) {
339
            // Wrapped with array_values to make array keys sequential after empty values removing
340 1
            $result = array_values(
341 1
                array_filter(
342 1
                    $result,
343 1
                    function ($value) {
344 1
                        return $value !== '';
345 1
                    }
346 1
                )
347 1
            );
348
        }
349
350 1
        return $result;
351
    }
352
353
    /**
354
     * Counts words in a string.
355
     *
356
     * @param string $string the text to calculate
357
     * @return int
358
     * @since 2.0.8
359
     */
360 2
    public static function countWords($string)
361
    {
362 2
        return count(preg_split('/\s+/u', $string, 0, PREG_SPLIT_NO_EMPTY));
363
    }
364
365
    /**
366
     * Returns string representation of number value with replaced commas to dots, if decimal point
367
     * of current locale is comma.
368
     *
369
     * @param int|float|string $value the value to normalize.
370
     * @return string
371
     * @since 2.0.11
372
     */
373 34
    public static function normalizeNumber($value)
374
    {
375 34
        $value = (string)$value;
376
377 34
        $localeInfo = localeconv();
378 34
        $decimalSeparator = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : null;
379
380 34
        if ($decimalSeparator !== null && $decimalSeparator !== '.') {
381 4
            $value = str_replace($decimalSeparator, '.', $value);
382
        }
383
384 34
        return $value;
385
    }
386
387
    /**
388
     * Encodes string into "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648).
389
     *
390
     * > Note: Base 64 padding `=` may be at the end of the returned string.
391
     * > `=` is not transparent to URL encoding.
392
     *
393
     * @param string $input the string to encode.
394
     * @return string encoded string.
395
     * @see https://tools.ietf.org/html/rfc4648#page-7
396
     * @since 2.0.12
397
     */
398 112
    public static function base64UrlEncode($input)
399
    {
400 112
        return strtr(base64_encode($input), '+/', '-_');
401
    }
402
403
    /**
404
     * Decodes "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648).
405
     *
406
     * @param string $input encoded string.
407
     * @return string decoded string.
408
     * @see https://tools.ietf.org/html/rfc4648#page-7
409
     * @since 2.0.12
410
     */
411 13
    public static function base64UrlDecode($input)
412
    {
413 13
        return base64_decode(strtr($input, '-_', '+/'));
414
    }
415
416
    /**
417
     * Safely casts a float to string independent of the current locale.
418
     * The decimal separator will always be `.`.
419
     *
420
     * @param float|int $number a floating point number or integer.
421
     * @return string the string representation of the number.
422
     * @since 2.0.13
423
     */
424 10
    public static function floatToString($number)
425
    {
426
        // . and , are the only decimal separators known in ICU data,
427
        // so its safe to call str_replace here
428 10
        return str_replace(',', '.', (string)$number);
429
    }
430
431
    /**
432
     * Checks if the passed string would match the given shell wildcard pattern.
433
     * This function emulates [[fnmatch()]], which may be unavailable at certain environment, using PCRE.
434
     *
435
     * @param string $pattern the shell wildcard pattern.
436
     * @param string $string the tested string.
437
     * @param array $options options for matching. Valid options are:
438
     *
439
     * - caseSensitive: bool, whether pattern should be case sensitive. Defaults to `true`.
440
     * - escape: bool, whether backslash escaping is enabled. Defaults to `true`.
441
     * - filePath: bool, whether slashes in string only matches slashes in the given pattern. Defaults to `false`.
442
     *
443
     * @return bool whether the string matches pattern or not.
444
     * @since 2.0.14
445
     */
446 244
    public static function matchWildcard($pattern, $string, $options = [])
447
    {
448 244
        if ($pattern === '*' && empty($options['filePath'])) {
449 5
            return true;
450
        }
451
452 240
        $replacements = [
453 240
            '\\\\\\\\' => '\\\\',
454 240
            '\\\\\\*'  => '[*]',
455 240
            '\\\\\\?'  => '[?]',
456 240
            '\*'       => '.*',
457 240
            '\?'       => '.',
458 240
            '\[\!'     => '[^',
459 240
            '\['       => '[',
460 240
            '\]'       => ']',
461 240
            '\-'       => '-',
462 240
        ];
463
464 240
        if (isset($options['escape']) && !$options['escape']) {
465 9
            unset($replacements['\\\\\\\\']);
466 9
            unset($replacements['\\\\\\*']);
467 9
            unset($replacements['\\\\\\?']);
468
        }
469
470 240
        if (!empty($options['filePath'])) {
471 12
            $replacements['\*'] = '[^/\\\\]*';
472 12
            $replacements['\?'] = '[^/\\\\]';
473
        }
474
475 240
        $pattern = strtr(preg_quote($pattern, '#'), $replacements);
476 240
        $pattern = '#^' . $pattern . '$#us';
477
478 240
        if (isset($options['caseSensitive']) && !$options['caseSensitive']) {
479 2
            $pattern .= 'i';
480
        }
481
482 240
        return preg_match($pattern, (string)$string) === 1;
483
    }
484
485
    /**
486
     * This method provides a unicode-safe implementation of built-in PHP function `ucfirst()`.
487
     *
488
     * @param string $string the string to be proceeded
489
     * @param string $encoding Optional, defaults to "UTF-8"
490
     * @return string
491
     * @see https://www.php.net/manual/en/function.ucfirst.php
492
     * @since 2.0.16
493
     * @phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
494
     */
495 229
    public static function mb_ucfirst($string, $encoding = 'UTF-8')
496
    {
497 229
        $firstChar = mb_substr((string)$string, 0, 1, $encoding);
498 229
        $rest = mb_substr((string)$string, 1, null, $encoding);
499
500 229
        return mb_strtoupper($firstChar, $encoding) . $rest;
501
    }
502
503
    /**
504
     * This method provides a unicode-safe implementation of built-in PHP function `ucwords()`.
505
     *
506
     * @param string $string the string to be proceeded
507
     * @param string $encoding Optional, defaults to "UTF-8"
508
     * @return string
509
     * @see https://www.php.net/manual/en/function.ucwords
510
     * @since 2.0.16
511
     * @phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
512
     */
513 226
    public static function mb_ucwords($string, $encoding = 'UTF-8')
514
    {
515 226
        $string = (string)$string;
516 226
        if (empty($string)) {
517 3
            return $string;
518
        }
519
520 223
        $parts = preg_split('/(\s+\W+\s+|^\W+\s+|\s+)/u', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
521 223
        $ucfirstEven = trim(mb_substr($parts[0], -1, 1, $encoding)) === '';
522 223
        foreach ($parts as $key => $value) {
523 223
            $isEven = (bool)($key % 2);
524 223
            if ($ucfirstEven === $isEven) {
525 223
                $parts[$key] = static::mb_ucfirst($value, $encoding);
526
            }
527
        }
528
529 223
        return implode('', $parts);
530
    }
531
532
    /**
533
     * Masks a portion of a string with a repeated character.
534
     * This method is multibyte-safe.
535
     *
536
     * @param string $string The input string.
537
     * @param int $start The starting position from where to begin masking.
538
     * This can be a positive or negative integer.
539
     * Positive values count from the beginning,
540
     * negative values count from the end of the string.
541
     * @param int $length The length of the section to be masked.
542
     * The masking will start from the $start position
543
     * and continue for $length characters.
544
     * @param string $mask The character to use for masking. The default is '*'.
545
     * @return string The masked string.
546
     */
547 1
    public static function mask($string, $start, $length, $mask = '*')
548
    {
549 1
        $strLength = mb_strlen($string, 'UTF-8');
550
551
        // Return original string if start position is out of bounds
552 1
        if ($start >= $strLength || $start < -$strLength) {
553 1
            return $string;
554
        }
555
556 1
        $masked = mb_substr($string, 0, $start, 'UTF-8');
557 1
        $masked .= str_repeat($mask, abs($length));
0 ignored issues
show
Bug introduced by
It seems like abs($length) can also be of type double; however, parameter $times of str_repeat() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

557
        $masked .= str_repeat($mask, /** @scrutinizer ignore-type */ abs($length));
Loading history...
558 1
        $masked .= mb_substr($string, $start + abs($length), null, 'UTF-8');
0 ignored issues
show
Bug introduced by
$start + abs($length) of type double is incompatible with the type integer expected by parameter $start of mb_substr(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

558
        $masked .= mb_substr($string, /** @scrutinizer ignore-type */ $start + abs($length), null, 'UTF-8');
Loading history...
559
560 1
        return $masked;
561
    }
562
563
    /**
564
     * Returns the portion of the string that lies between the first occurrence of the start string
565
     * and the last occurrence of the end string after that.
566
     *
567
     * @param string $string The input string.
568
     * @param string $start The string marking the start of the portion to extract.
569
     * @param string $end The string marking the end of the portion to extract.
570
     * @return string|null The portion of the string between the first occurrence of
571
     * start and the last occurrence of end, or null if either start or end cannot be found.
572
     */
573 12
    public static function findBetween($string, $start, $end)
574
    {
575 12
        $startPos = mb_strpos($string, $start);
576
577 12
        if ($startPos === false) {
578 3
            return null;
579
        }
580
581 9
        $startPos += mb_strlen($start);
582 9
        $endPos = mb_strrpos($string, $end, $startPos);
583
584 9
        if ($endPos === false) {
585 2
            return null;
586
        }
587
588 7
        return mb_substr($string, $startPos, $endPos - $startPos);
589
    }
590
}
591