From 41401a6de496768cbfec6b27d3530857bf9f184c Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 27 Oct 2021 13:15:19 +0200 Subject: [PATCH] Strings::split() added parameter $limit --- src/Utils/Strings.php | 3 ++- tests/Utils/Strings.split().phpt | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Utils/Strings.php b/src/Utils/Strings.php index 6e3aa0438..cca621e16 100644 --- a/src/Utils/Strings.php +++ b/src/Utils/Strings.php @@ -494,12 +494,13 @@ public static function split( string $pattern, bool|int $captureOffset = false, bool $skipEmpty = false, + int $limit = -1, ): array { $flags = is_int($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]); } diff --git a/tests/Utils/Strings.split().phpt b/tests/Utils/Strings.split().phpt index 3a57a4c4e..9638b2710 100644 --- a/tests/Utils/Strings.split().phpt +++ b/tests/Utils/Strings.split().phpt @@ -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));