From dfa0e4790a94a57c2240d8152669bc4ebda7ec4a Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 19 Apr 2022 13:05:52 +0200 Subject: [PATCH] wip #5189 @0.25 --- src/ArrayUtil.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/ArrayUtil.php b/src/ArrayUtil.php index 7a602a4..edb1e5f 100644 --- a/src/ArrayUtil.php +++ b/src/ArrayUtil.php @@ -162,4 +162,34 @@ class ArrayUtil { return $res; } + /** + * @param $str + * @return int[] + */ + public static function parseRange($str) + { + $res = array(); + $str = preg_replace('|([^0-9;\-,]?)|', '', $str); + if ($str == '') { + return $res; + } + $ranges = Text::multiExplode(';,', $str); + foreach($ranges as $range) { + $e = explode('-', $range); + if (count($e) == 1) { + $res[] = intval($e[0]); + } else { + if ($e[0] > $e[1]) { + $res = array_merge($res, range($e[0], $e[1])); + } else { + $res = array_merge($res, range($e[1], $e[0])); + } + } + } + $res = array_unique($res); + sort($res, SORT_NUMERIC); + return $res; + } + + } -- 2.39.5