From 215c694bea0b99aa76d22fb69e9343cfcb36f659 Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Tue, 22 Dec 2020 10:16:12 +0700 Subject: [PATCH] 4th argument to re-split to if empty strings are removed (default to true) --- src/Lib/Regex.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Lib/Regex.php b/src/Lib/Regex.php index 54a64df..0be9623 100644 --- a/src/Lib/Regex.php +++ b/src/Lib/Regex.php @@ -42,11 +42,20 @@ class Regex implements ILib )); $env->set('re-replace', new CoreFunc('re-replace', 'Replace matches to regular expression (first argument) by the second argument in the subject (third argument).', 3, 4, - fn (string $pattern, string $replacement, string $subject, int $limit = -1) => preg_replace($pattern, $replacement, $subject, $limit) + function (string $pattern, string $replacement, string $subject, int $limit = -1) { + return preg_replace($pattern, $replacement, $subject, $limit); + } )); - $env->set('re-split', new CoreFunc('re-split', 'Split second argument by regular expression given as first argument.', 2, 3, - fn (string $pattern, string $subject, int $limit = -1) => new Vector(preg_split($pattern, $subject, $limit, PREG_SPLIT_NO_EMPTY)) + $env->set('re-split', new CoreFunc('re-split', 'Split second argument by regular expression given as first argument.', 2, 4, + function (string $pattern, string $subject, int $limit = -1, bool $removeEmpty = true) { + $flags = 0; + if ($removeEmpty) { + $flags = PREG_SPLIT_NO_EMPTY; + } + + return new Vector(preg_split($pattern, $subject, $limit, $flags)); + } )); } }