【问题标题】:php transforming a specific string into array valuesphp将特定字符串转换为数组值
【发布时间】:2011-01-17 19:53:24
【问题描述】:

我从服务器得到这个响应:

OK: 0; Sent queued message ID: e3674786a1c5f7a1 SMSGlobalMsgID:6162865783958235 OK: 0; Sent queued message ID: 9589936487b8d0a1 SMSGlobalMsgID:6141138716371692 

等等……

这只是一个没有回车的长字符串,我完全按照收到的方式复制了它。 请注意,初始 OK 可以是不同的单词,0(在字符串的开头)最多可以是 3 位数字。

这是重复的模式:

OK: 0; Sent queued message ID: e3674786a1c5f7a1 SMSGlobalMsgID:6162865783958235

我想将它转换成如下所示的数组:

Array
(
    [0] => Array
           [1] => OK
           [2] => 0
           [3] => e3674786a1c5f7a1
           [4] => 6162865783958235

    [1] => Array
           [1] => OK
           [2] => 0
           [3] => 9589936487b8d0a1
           [4] => 6141138716371692 
)

你会怎么做?感谢您的意见。

【问题讨论】:

    标签: php arrays string multidimensional-array


    【解决方案1】:

    考虑到您将该数据作为字符串作为输入:

    $str = <<<STR
    OK: 0; Sent queued message ID: e3674786a1c5f7a1 SMSGlobalMsgID:6162865783958235
    OK: 0; Sent queued message ID: 9589936487b8d0a1 SMSGlobalMsgID:6141138716371692
    STR;
    


    一个解决方案是将explode 该字符串分成单独的行:

    $lines = explode("\n", $str);
    

    在评论和OP的编辑之后编辑

    考虑到您收到的数据仅在一行上,您必须找到另一种拆分方法(我认为拆分数据并处理处理大量数据的“行”更容易一次)

    考虑到您收到的数据如下所示:

    $str = <<<STR
    OK: 0; Sent queued message ID: e3674786a1c5f7a1 SMSGlobalMsgID:6162865783958235 OK: 0; Sent queued message ID: 9589936487b8d0a1 SMSGlobalMsgID:6141138716371692
    STR;
    

    我想你可以用preg_split 将它分成“行”,使用像这样的正则表达式:

    $lines = preg_split('/SMSGlobalMsgID: (\d+) /', $str);
    

    如果您尝试输出$lines 的内容,看起来还不错——您现在应该可以遍历这些行了。


    然后,首先将$output 数组初始化为空数组:

    $output = array();
    


    现在你必须遍历初始输入的行,在每一行上使用一些正则表达式魔法:
    更多信息请参阅preg_matchRegular Expressions (Perl-Compatible) 的文档

    foreach ($lines as $line) {
      if (preg_match('/^(\w+): (\d+); Sent queued message ID: ([a-z0-9]+) SMSGlobalMsgID:(\d+)$/', trim($line), $m)) {
        $output[] = array_slice($m, 1);
      }
    }
    

    注意我使用()捕获的部分


    最后,你会得到 $output 数组:

    var_dump($output);
    

    看起来像这样:

    array
      0 => 
        array
          0 => string 'OK' (length=2)
          1 => string '0' (length=1)
          2 => string 'e3674786a1c5f7a1' (length=16)
          3 => string '6162865783958235' (length=16)
      1 => 
        array
          0 => string 'OK' (length=2)
          1 => string '0' (length=1)
          2 => string '9589936487b8d0a1' (length=16)
          3 => string '6141138716371692' (length=16)
    

    【讨论】:

    • 感谢您的回答,但正如我所说,没有回车,所以 \n 将不起作用。 + 初始 OK 可以是不同的值,0 可以是最多 3 位数字。对不起,我没有说明,我会更新帖子
    • 哦 :-( 我只是从你的问题中复制粘贴了字符串,它在两行上,在你编辑之前 ^^ ;;我会尝试几个想法,然后编辑我的答案如果我发现一些有用的东西:-) (编辑我的评论:我刚刚编辑了我的答案以提供一些附加信息——现在应该与非多行输入一起使用)
    • "initial OK 可以是不同的值,0 最多可以是 3 位数字" >> 我的代码应该适用,因为我匹配了 \d+,这意味着“至少一个数字”
    • 试过了,vardump -> array(0) { } 明天我会研究一下...如果我可以使用它,请勾选您的解决方案。
    • 首先它失败了 $lines = preg_split('/SMSGlobalMsgID: (\d+) /', $str);冒号后面没有空格。然后在结果中它没有显示 SMSGlobalID 因为它被 preg_split 删除,然后我只输出一个数组,但我猜这是因为该行与 preg_match 模式不匹配(因为缺少 SMSGlobalID)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 2013-08-12
    • 2021-12-27
    • 1970-01-01
    相关资源
    最近更新 更多