php 正则表达式匹配问题
php regex matching issue
我有这样的输出:
1342&-4&-6
它可能更多或更少:
1342&-4(我需要将其替换为:1342,1344)
1340&-1&-3&-5&-7(我需要替换成:1340,1341,1343,1345,1347)
我尝试使用 preg_match 但没有成功,
有人可以帮我吗?
谢谢,
$array = explode('&-', $string);
$len = count($array);
for($i=1; $i<$len; $i++)
$array[$i] += $array[0] / 10 * 10;
var_dump(implode(' ', $array));
<?php
//so if you had sting input of "1342&-4" you would get 2 stings returned "1342" and "1344"?
//1340&-1&-3&-5&-7 --> 1340 and 1341 and 1343 and 1345 and 1347
//$str="1342&-4";
$str="1340&-1&-3&-5&-7";
$x=explode('&-',$str);
//print_r($x);
foreach ($x as $k=> $v){
if($k==0){
//echo raw
echo $v."<br>";
}else{
//remove last number add new last number
echo substr($x[0], 0, -1).$v."<br>";
}
}
输出:
1340
1341
1343
1345
1347
我用过<br>
你可以使用任何你需要的或添加到一个新变量(数组)
我有这样的输出: 1342&-4&-6 它可能更多或更少: 1342&-4(我需要将其替换为:1342,1344) 1340&-1&-3&-5&-7(我需要替换成:1340,1341,1343,1345,1347)
我尝试使用 preg_match 但没有成功, 有人可以帮我吗?
谢谢,
$array = explode('&-', $string);
$len = count($array);
for($i=1; $i<$len; $i++)
$array[$i] += $array[0] / 10 * 10;
var_dump(implode(' ', $array));
<?php
//so if you had sting input of "1342&-4" you would get 2 stings returned "1342" and "1344"?
//1340&-1&-3&-5&-7 --> 1340 and 1341 and 1343 and 1345 and 1347
//$str="1342&-4";
$str="1340&-1&-3&-5&-7";
$x=explode('&-',$str);
//print_r($x);
foreach ($x as $k=> $v){
if($k==0){
//echo raw
echo $v."<br>";
}else{
//remove last number add new last number
echo substr($x[0], 0, -1).$v."<br>";
}
}
输出:
1340
1341
1343
1345
1347
我用过<br>
你可以使用任何你需要的或添加到一个新变量(数组)