【问题标题】:Convert number sequence to array in PHP在PHP中将数字序列转换为数组
【发布时间】:2012-05-18 22:57:00
【问题描述】:

有点像 PHP 和 Regex 的菜鸟,我从 Web 服务收到以下信息:

test:002005@1111@333333@;10205@2000@666666@;002005@1111@55555@;

上面的行是一个由 3 个数字组成的序列,重复 3 次。我想获得每个序列的第三个数字,我相信最好的课程(除了 3000 个爆炸)将是 preg_match_all,但我很难将注意力集中在 RegEx 上。

最终结果应该是这样的:

 Array
    (
        [0] => 333333
        [1] => 666666
        [2] => 55555
    )

提前感谢您的帮助。

【问题讨论】:

    标签: php regex arrays string preg-match-all


    【解决方案1】:
    if(preg_match_all('/.*?(?:\d+@){2}(\d+)@;/',$s,$m)) {
            print_r($m[1]);
    }
    

    http://ideone.com/99M9t

    你可以使用explode as来做到这一点:

    $input = rtrim($input,';');
    $temp1 = explode(';',$input);
    foreach($temp1 as $val1) {
            $temp2 = explode('@',$val1);
            $result[] = $temp2[2];
    }
    print_r($result);
    

    http://ideone.com/VH29g

    【讨论】:

    • 非常感谢,成功了。我喜欢这个,因为它定义了一个事实,即在需要的那个之前有 2 个“@XXXX”。干得好!
    【解决方案2】:

    使用函数explode()

    <?php
    
    $pizza  = "piece1@piece2@piece3@piece4@piece5@piece6";
    $pieces = explode("@", $pizza);
    echo $pieces[0]; // piece1
    echo $pieces[1]; // piece2
    
    ?>
    

    【讨论】:

    • 他要求一个没有爆炸的版本,我认为他的输入非常大,他想要一个快速/可扩展的解决方案。 (我现在不能说任何一种方法的运行时间。)
    • @Taz:他要求一个没有 3000 个爆炸的版本,我只看到 1 个。我相信这很好地满足了这个要求。虽然这实际上并没有得到他要求的输出......
    • 我相信他需要再次爆炸才能获得每个$piece 中的第三个项目。这种爆炸将是一个循环,所以你会收到 n+1 explode 电话。
    【解决方案3】:

    我不记得这句话是怎么说的了,但是……

    “你有一个问题,决定使用正则表达式……现在你有两个问题。”

    如果我们假设 'test:' 不是要解析的实际字符串的一部分,您的问题很容易解决。

    <?php
    
    $in = '002005@1111@333333@;10205@2000@666666@;002005@1111@55555@;';
    
    function splitGroupsAndGetColumn($input, $groupSeparator, $columnSeparator, $columnIndex, $skipEmpty=true)
    {
        $result = array();
    
        $groups = explode($groupSeparator, $input);
        foreach($groups as $group)
        {
            $columns = explode($columnSeparator, $group);
            if (isset($columns[$columnIndex]))
            {
                array_push($result, $columns[$columnIndex]);
            }
            else if (! $skipEmpty)
            {
                array_push($result, NULL);
            }
        }
    
        return $result;
    }
    
    var_dump(splitGroupsAndGetColumn($in, ';', '@', 2));
    

    输出:

    array(3) {
      [0]=>
      string(6) "333333"
      [1]=>
      string(6) "666666"
      [2]=>
      string(5) "55555"
    }
    

    【讨论】:

    • 这句话一定是来自不了解正则表达式的人;-) ...至少:我想知道是什么让有人认为对于这样一个简单的任务,循环比正则表达式更好?
    • 我不知道,但我喜欢正则表达式几乎和原来的说法一样多。正则表达式很好恕我直言,但不能像众所周知的锤子那样使用。
    • 我在某本书中看到了一个用于验证跨越几页的电子邮件地址的正则表达式。我认为这是我同意你的情况:-)
    • @harald:完全正确。 /([b]{2}|[^b]{2})/
    • @YagoRiveiro:确实。如果你问我,也可以无限阅读。
    【解决方案4】:

    你可以使用preg_match_all来完成这个任务,这使得任务变得非常简单:

    $a = "test:002005@1111@333333@;10205@2000@666666@;002005@1111@55555@;";
    
    preg_match_all('/@(\d+)@;/', $a, $m);
    
    print_r($m);
    

    $m[1] 包含你想要的输出。

    参考:http://php.net/manual/en/function.preg-match-all.php

    【讨论】:

      【解决方案5】:

      我的版本:)

      正则表达式 (\d+) 表示我想要所有数字为 1 或多个

      php > $a = '002005@1111@333333@;10205@2000@666666@;002005@1111@55555@';
      php > preg_match_all('/(\d+)/',$a,$matches);
      php > var_dump($matches);
      array(2) {
        [0]=>
        array(9) {
          [0]=>
          string(6) "002005"
          [1]=>
          string(4) "1111"
          [2]=>
          string(6) "333333"
          [3]=>
          string(5) "10205"
          [4]=>
          string(4) "2000"
          [5]=>
          string(6) "666666"
          [6]=>
          string(6) "002005"
          [7]=>
          string(4) "1111"
          [8]=>
          string(5) "55555"
        }
        [1]=>
        array(9) {
          [0]=>
          string(6) "002005"
          [1]=>
          string(4) "1111"
          [2]=>
          string(6) "333333"
          [3]=>
          string(5) "10205"
          [4]=>
          string(4) "2000"
          [5]=>
          string(6) "666666"
          [6]=>
          string(6) "002005"
          [7]=>
          string(4) "1111"
          [8]=>
          string(5) "55555"
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-08-09
        • 1970-01-01
        • 2015-05-05
        • 2012-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-16
        相关资源
        最近更新 更多