简体   繁体   English

php需要正则表达式方面的帮助

[英]php need assistance with regular expression

I want to parse and expand the given strings in PHP.我想在 PHP 中解析和扩展给定的字符串。

From

0605052&&-5&-7&-8
0605052&&-4&-7
0605050&&-2&-4&-6&-8

To

0605052, 0605053 ,0605054 ,0605055, 0605057, 0605058
0605052,0605053,0605054,0605057
0605050,0605051,0605052,0605054,0605056,0605058

can someone help me with that?有人可以帮我吗? thanks in advance!提前致谢!

Your question is not very clear, but I think you mean a solution like this:你的问题不是很清楚,但我认为你的意思是这样的解决方案:

Edited: Now the hole ranges were shown and not only the specified numbers.编辑:现在显示孔范围,而不仅仅是指定的数字。

<?php
    $string = "0605052&&-5&-7&-8";
    $test = '/^([0-9]+)\&+/';
    preg_match($test, $string, $res);

    if (isset($res[1]))
    {
        $nr = $res[1];

        $test = '/\&\-([0-9])/';
        preg_match_all($test, $string, $res);

        $result[] = $nr;
        $nrPart = substr($nr, 0, -1);
        $firstPart = substr($nr, -1);

        if (isset($res[1]))
        {
            foreach ($res[1] as &$value)
            {
                if ($firstPart !== false)
                {
                    for ($i=$firstPart+1; $i<=$value; $i++)
                    {
                        $nr = $nrPart . $i;
                        $result[] = $nr;
                    }
                    $firstPart = false;
                }
                else
                {
                    $nr = $nrPart . $value;
                    $result[] = $nr;
                    $firstPart = $value;
                }
            }
        }
        var_dump($result);
    }
?>

This delivers:这提供:
result[0] = "0605052"结果[0] =“0605052”
result[1] = "0605053"结果[1] = "0605053"
result[2] = "0605054"结果[2] =“0605054”
result[3] = "0605055"结果[3] = "0605055"
result[4] = "0605057"结果[4] =“0605057”
result[5] = "0605058"结果[5] =“0605058”

I think a multi step approach is the best thing to do here.我认为多步骤方法是最好的方法。

Eg take this as an example 0605052&&-5&-7&-8 :例如以0605052&&-5&-7&-8为例:

  1. Split at - .-拆分。 The result will be 0605052&& , 5& , 7& , 8结果将是0605052&& , 5& , 7& , 8
  2. The first result 0605052&& will help you create your base.第一个结果0605052&&将帮助您创建基础。 Simply substring the numbers by finding first occurence of & and substring to the next to last number.只需通过查找&第一次出现和子字符串到倒数第二个数字来对数字进行子串。 Result will be 060505 .结果将是060505 You will also need the last number, so get it as well (which is 2 in this case).您还需要最后一个数字,所以也要获取它(在本例中为2 )。
  3. Get the remaining ends now, all \\d& are simple to get, simply take the first character of the string (or if those can be more than one number, use substring with first occurence of & approach again).现在获取剩余的结尾,所有\\d&都很容易获取,只需取字符串的第一个字符(或者如果这些字符可以是多个数字,则再次使用第一次出现&方法的子字符串)。
  4. The last number is simple: it is 8 .最后一个数字很简单:它是8

Now you got all important values.现在你得到了所有重要的值。 You can generate your result:您可以生成结果:

The last number from 2., all numbers from 3. and the number from 4. together with your base are the first part. 2. 中的最后一个数字、3. 中的所有数字和 4. 中的数字以及您的基数是第一部分。 In addition, you need to generate all numbers from the last number of 2. and the first result of 3. in a loop by a step of 1 and append it to your base.此外,您需要从 2. 的最后一个数字和 3. 的第一个结果以 1 为步长在循环中生成所有数字,并将其附加到您的基数中。

Example Code:示例代码:

<?php
$str = '0605052&&-5&-7&-8';
$split = explode('-', $str);

$firstAmpBase = strpos($split[0], '&');
$base = substr($split[0], 0, $firstAmpBase - 1);
$firstEnd = substr($split[0], $firstAmpBase - 1, 1);
$ends = [];

$firstSingleNumber = substr($split[1], 0, strpos($split[1], '&'));
for ($i = $firstEnd; $i < $firstSingleNumber; $i++) {
    array_push($ends, $i);
}
array_push($ends, $firstSingleNumber);

for ($i = 2; $i < count($split) - 1; $i++) {
    array_push($ends, substr($split[$i], 0, strpos($split[$i], '&')));
}
array_push($ends, $split[count($split) - 1]);

foreach ($ends as $end) {
    echo $base . $end . '<br>';
}
?>

Output:输出:

0605052
0605053
0605054
0605055
0605057
0605058

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

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