]> _ Git - cubist_util.git/commitdiff
wip #5189 @0.25
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 19 Apr 2022 11:05:52 +0000 (13:05 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 19 Apr 2022 11:05:52 +0000 (13:05 +0200)
src/ArrayUtil.php

index 7a602a48c9324dae153365b405197dc92296273c..edb1e5f9577edf3c113d3f9fe0ebd3232b476d3b 100644 (file)
@@ -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;
+    }
+
+
 }