简体   繁体   English

PHP正则表达式匹配问题

[英]php regex matching issue

I have this kind of output: 1342&-4&-6 And it could be more times or less: 1342&-4 (I need to replace it with: 1342,1344) 1340&-1&-3&-5&-7 (I need to replace it with: 1340,1341,1343,1345,1347) 我有这种输出:1342&-4&-6可能会更多或更少:1342&-4(我需要替换为:1342,1344)1340&-1&-3&-5&-7(我需要替换它与:1340,1341,1343,1345,1347)

I've tried to use preg_match but with no success, Can someone help me with that? 我尝试使用preg_match,但没有成功,有人可以帮助我吗?

Thanks, 谢谢,

$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>"; 
    }
}

output: 输出:

1340 1340
1341 1341
1343 1343
1345 1345
1347 1347

i used <br> you can use what ever you need or add to a new variable(array) 我用过<br>您可以使用任何您需要的东西或添加到新变量(数组)中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM