php str 4-6 40-46 comma explode str array array foreach comma as value

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
$str = '1,4-6,7,40-46/2';
$comma = explode(',', $str);
$array = array();
foreach ($comma as $value) {
if (preg_match('#^([0-9]+)\-([0-9]+)/([0-9]+)$#', $value, $match)) {
$start = (int) $match[1];
$limit = (int) $match[2];
$step = (int) $match[3];
for ($i = $start; $i <= $limit; $i += $step) {
$array[] = $i;
}
} else if (preg_match('#^([0-9]+)\-([0-9]+)$#', $value, $match)) {
$start = (int) $match[1];
$limit = (int) $match[2];
for ($i = $start; $i <= $limit; ++$i) {
$array[] = $i;
}
} else if (preg_match('#^([0-9]+)$#', $value, $match)) {
$array[] = (int) $match[0];
}
}
var_dump($array);