Skip to content

Commit

Permalink
Strings::split() added parameter $limit
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 27, 2021
1 parent a08d1e4 commit 619eb3e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Utils/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,11 +474,12 @@ public static function split(
string $pattern,
bool|int $captureOffset = false,
bool $skipEmpty = false,
int $limit = -1,
): array {
$flags = is_int($captureOffset) && $captureOffset // back compatibility
? $captureOffset
: ($captureOffset ? PREG_SPLIT_OFFSET_CAPTURE : 0) | ($skipEmpty ? PREG_SPLIT_NO_EMPTY : 0);
return self::pcre('preg_split', [$pattern, $subject, -1, $flags | PREG_SPLIT_DELIM_CAPTURE]);
return self::pcre('preg_split', [$pattern, $subject, $limit, $flags | PREG_SPLIT_DELIM_CAPTURE]);
}


Expand Down
2 changes: 2 additions & 0 deletions tests/Utils/Strings.split().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ Assert::same([
[',', 4],
['c', 6],
], Strings::split('a, b, c', '#(,)\s*#', captureOffset: true));

Assert::same(['a', ',', 'b, c'], Strings::split('a, b, c', '#(,)\s*#', limit: 2));

0 comments on commit 619eb3e

Please sign in to comment.