For my current project I need an user to define a range of numbers which is stored in a database.
对于我当前的项目,我需要用户定义存储在数据库中的一系列数字。
The following string is a possible user input:
以下字符串是可能的用户输入:
1025-1027,1030,1032-1034
I want to process this string with php to get an array of possible numbers including the possibility to add a range of numbers with n-n²
or adding single numbers separated with n; n²
which for this example would be:
我想用php处理这个字符串以获得一系列可能的数字,包括添加n-n²的数字范围或添加用n分隔的单个数字的可能性;对于这个例子,n²是:
1025 1026 1027 1030 1032 1031 1034
2 个解决方案
#1
5
Split the input string by comma and then see what each element of the new array is. If it has the range delimiter (-
), add each number from the range to the array:
用逗号分隔输入字符串,然后查看新数组的每个元素是什么。如果它具有范围分隔符( - ),则将范围中的每个数字添加到数组中:
$input = '1025-1027,1030,1032-1034';
$inputArray = explode(',', $input);
$outputArray = array();
foreach($inputArray as $v) {
if(strpos($v, '-') === false) {
$outputArray[] = $v;
} else {
$minMax = explode('-',$v);
if($minMax[0]<=$minMax[1]) {
$outputArray = array_merge($outputArray, range($minMax[0], $minMax[1]));
}
}
}
print_r($outputArray);
The value returned in the end is
最后返回的值是
Array
(
[0] => 1025
[1] => 1026
[2] => 1027
[3] => 1030
[4] => 1032
[5] => 1033
[6] => 1034
)
#2
3
Another way / variant would be to explode
both ,
and -
, then map each exploded group then use range
, after the ranges has been created, remerge the grouping:
另一种方式/变体是爆炸两者,然后 - ,然后映射每个爆炸组,然后使用范围,在创建范围后,重新合并分组:
$input = '1025-1027,1030,1032-1034';
// explode, map, explode, create range
$numbers = array_map(function($e){
$range = explode('-', $e);
return (count($range) > 1) ? range(min($range), max($range)) : $range;
}, explode(',', $input));
// re merge
$numbers = call_user_func_array('array_merge', $numbers);
print_r($numbers);
#1
5
Split the input string by comma and then see what each element of the new array is. If it has the range delimiter (-
), add each number from the range to the array:
用逗号分隔输入字符串,然后查看新数组的每个元素是什么。如果它具有范围分隔符( - ),则将范围中的每个数字添加到数组中:
$input = '1025-1027,1030,1032-1034';
$inputArray = explode(',', $input);
$outputArray = array();
foreach($inputArray as $v) {
if(strpos($v, '-') === false) {
$outputArray[] = $v;
} else {
$minMax = explode('-',$v);
if($minMax[0]<=$minMax[1]) {
$outputArray = array_merge($outputArray, range($minMax[0], $minMax[1]));
}
}
}
print_r($outputArray);
The value returned in the end is
最后返回的值是
Array
(
[0] => 1025
[1] => 1026
[2] => 1027
[3] => 1030
[4] => 1032
[5] => 1033
[6] => 1034
)
#2
3
Another way / variant would be to explode
both ,
and -
, then map each exploded group then use range
, after the ranges has been created, remerge the grouping:
另一种方式/变体是爆炸两者,然后 - ,然后映射每个爆炸组,然后使用范围,在创建范围后,重新合并分组:
$input = '1025-1027,1030,1032-1034';
// explode, map, explode, create range
$numbers = array_map(function($e){
$range = explode('-', $e);
return (count($range) > 1) ? range(min($range), max($range)) : $range;
}, explode(',', $input));
// re merge
$numbers = call_user_func_array('array_merge', $numbers);
print_r($numbers);