1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the mo4-coding-standard (phpcs standard) |
5
|
|
|
* |
6
|
|
|
* @author Michael Moll <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @license http://spdx.org/licenses/MIT MIT License |
9
|
|
|
* |
10
|
|
|
* @link https://github.com/mayflower/mo4-coding-standard |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace MO4\Library; |
16
|
|
|
|
17
|
|
|
use PHP_CodeSniffer\Exceptions\RuntimeException; |
18
|
|
|
|
19
|
|
|
class PregLibrary |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Split string by a regular expression |
23
|
|
|
* |
24
|
|
|
* @param string $pattern The pattern to search for, as a string. |
25
|
|
|
* @param string $subject The input string. |
26
|
|
|
* @param int $limit If specified, then only substrings up to limit are returned with the rest of the string |
27
|
|
|
* being placed in the last substring. A limit of -1, 0 or NULL means "no limit" and, as is |
28
|
|
|
* standard across PHP, you can use NULL to skip to the flags parameter. |
29
|
|
|
* @param int $flags Can be any combination of the following flags (combined with the | bitwise operator): |
30
|
|
|
* PREG_SPLIT_NO_EMPTY: If this flag is set, only non-empty pieces will be returned. |
31
|
|
|
* PREG_SPLIT_DELIM_CAPTURE: If this flag is set, parenthesized expression in the delimiter |
32
|
|
|
* pattern will be captured and returned as well. |
33
|
|
|
* PREG_SPLIT_OFFSET_CAPTURE: If this flag is set, for every occurring match the appendant |
34
|
|
|
* string offset will also be returned. |
35
|
|
|
* |
36
|
|
|
* @return array<string>|array<array> |
37
|
|
|
* |
38
|
|
|
* @throws RuntimeException |
39
|
|
|
* |
40
|
|
|
* @psalm-suppress ArgumentTypeCoercion |
41
|
|
|
*/ |
42
|
|
|
public static function MO4PregSplit(string $pattern, string $subject, int $limit = -1, int $flags = 0): array |
43
|
|
|
{ |
44
|
|
|
$pregSplitResult = \preg_split($pattern, $subject, $limit, $flags); |
45
|
|
|
|
46
|
|
|
// @phan-suppress-next-line PhanTypeComparisonToArray |
47
|
|
|
if (false === $pregSplitResult) { |
48
|
|
|
throw new RuntimeException('Unexpected Error in MO4 Coding Standard.'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $pregSplitResult; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|