Reputation: 15
i would like to calculate the differences between two given numbers for example:
1 - 5 or 24 - 35
I have to separate the numbers in arrays and to write the differences between them like:
1 2 3 4 5 or 24 25 26 27 28 29 30 31 32 33 34 35
is there any php function that can handle that or any other php solution?
my code:
$string = array('1 - 5','9','11','24 - 35');
$arr = explode(",", $string);
foreach ($arr as $val) {
if (preg_match("/ - /", $val)) {
$val = trim($val);
$min= trim(substr($val, 0, 2));
$max = trim(substr($val, -2));
}
}
Thanks a lot!
Upvotes: 0
Views: 3973
Reputation: 94672
You were using implode on the array unnecessarily. you need to process the array as an array to start with.
With the use of range()
you can generate the numbers between x - y
quite nicely.
There is also no need to get the regex engine involved in something as simple as is there a hyphen in this string
$string = array('1 - 5','9','11','24 - 35');
$new_arr = array();
foreach ($string as $val) {
if (strpos($val,'-') !== false) {
$v2 = explode('-', $val);
foreach (range($v2[0],$v2[1]) as $v)
$new_arr[] = $v;
} else {
$new_arr[] = $val;
}
}
print_r($new_arr);
RESULTS:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 9
[6] => 11
[7] => 24
[8] => 25
[9] => 26
[10] => 27
[11] => 28
[12] => 29
[13] => 30
[14] => 31
[15] => 32
[16] => 33
[17] => 34
[18] => 35
)
Upvotes: 0
Reputation: 40639
Your $string
is not a string it is a array so no need to explode array and try to use preg_split() to split strings. Use range() and implode() without using loop like,
<?php
$arr = array('1 - 5','9','11','24 - 35');
foreach ($arr as $val) {
$range = preg_split("/ - /", $val);
if(count($range)==2 && $range[0]<$range[1]){
echo implode(',',range($range[0],$range[1]));
echo "\n";
}
}
?>
Demo with Range without using Loop
Upvotes: 0
Reputation: 771
hope it helps
$string = array('1 - 5','9','11','24 - 35');
foreach ($string as $key => $val) {
preg_match_all("/[0-9]+/", $val, $matches);
if(count($matches[0]) == 1) {
//single number
$string[$key] = $matches[0][0];
} elseif(count($matches[0]) == 2) {
$n1 = intval($matches[0][0]);
$n2 = intval($matches[0][1]);
$vals = range($n1, $n2);
$string[$key] = implode(" ", $vals);
} else {
throw new \RuntimeException("more than 2 numbers");
}
}
print_r($string);
result:
Array
(
[0] => 1 2 3 4 5
[1] => 9
[2] => 11
[3] => 24 25 26 27 28 29 30 31 32 33 34 35
)
Upvotes: 0
Reputation: 337
$string = '1 - 5,9,11,24 - 35';
$arr = explode(',', $string);
foreach ($arr as $val) {
if (count($values = explode('-', $val)) === 2) {
$range = range(min(trim($values[0]), trim($values[1])), max(trim($values[0]), trim($values[1])));
var_dump($range);
}
}
Upvotes: 0
Reputation: 43497
Use range()
to get array of numbers in range.
<?php
$numbers = ['1 - 5','9','11','24 - 35'];
$newNumbers = [];
foreach ($numbers as $i => $num) {
$numRange = explode('-', str_replace(' ', '', $num));
if (count($numRange) != 1) {
$newNumbers = array_merge($newNumbers, range($numRange[0], $numRange[1]));
} else {
$newNumbers[] = $num;
}
}
var_dump($newNumbers);
/**
Outputs
array(19) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
[4]=>
int(5)
[5]=>
int(9)
[6]=>
int(11)
[7]=>
int(24)
[8]=>
int(25)
[9]=>
int(26)
[10]=>
int(27)
[11]=>
int(28)
[12]=>
int(29)
[13]=>
int(30)
[14]=>
int(31)
[15]=>
int(32)
[16]=>
int(33)
[17]=>
int(34)
[18]=>
int(35)
}
Upvotes: 0