douaipi3965 2015-06-21 10:14
浏览 122
已采纳

将字符串解析为命令行的数组输出

I'm working on a new Symfony 2 project which will be a panel management for Docker containers.

In this project, I'm executing some commands with the exec() PHP function.

I'm trying to parse the output of the following command :

docker create tutum/lamp:latest --name test 2>&1

When the command is a success I'm getting the container ID in a string which is good and easy to use but when a problem occured it's not the same. The result is a string with a var="data" syntax which I want to parse in order to get an array.

The command output :

time="2015-06-21T11:33:26+02:00" level="fatal" msg="Error response from daemon: Conflict. The name \"test\" is already in use by container XXXXXXXX. You have to delete (or rename) that container to be able to reuse that name."

I wish to have something like that :

Array( time => "2015-06-21T11:33:26+02:00", level => "fatal" ...);

I know that I have to do a regex parsing. After a while (regex and me are not realy best friends) I get this regex (tested on https://regex101.com/) :

/([a-zA-Z]+)="((.*)*)"/

I used preg_split function i'm not sure that it's the good one.

preg_split('/([a-zA-Z]+)="((.*)*)"/', $output)

Result is :

array(2) { [0]=> string(0) "" [1]=> string(0) "" }

Have you any suggestions to help me ? Many thanks for your help.

  • 写回答

2条回答 默认 最新

  • douzhang1926 2015-06-21 11:08
    关注

    TL;DR: This should work:

    preg_match_all(',([a-z]+)="((?:[^"]|\\\\")*[^\\\\])",', $a, $matches, PREG_SET_ORDER);
    var_dump($matches);
    

    The last var_dump prints the following data structure, which should be easy to process:

    array(3) {
      [0] => array(3) {
        [0] => string(32) "time="2015-06-21T11:33:26+02:00""
        [1] => string(4) "time"
        [2] => string(25) "2015-06-21T11:33:26+02:00"
      }
      [1] => array(3) {
        [0] => string(13) "level="fatal""
        [1] => string(5) "level"
        [2] => string(5) "fatal"
      }
      [2] => array(3) {
        [0] => string(179) "msg="Error response from daemon: Conflict. The name \\"test\\" is already in use by container XXXXXXXX. You have to delete (or rename) that container to be able to reuse that name.""
        [1] => string(3) "msg"
        [2] => string(173) "Error response from daemon: Conflict. The name \\"test\\" is already in use by container XXXXXXXX. You have to delete (or rename) that container to be able to reuse that name."
      }
    }
    

    Why this works

    The regular expression explained:

    ([a-z]+)                    # Match the label ("time", "level" or "msg")
    =                           # Self-explanatory
    "((?:[^"]|\\\\")*[^\\\\])"  # This is the tricky part:
                                # Match the quoted string; this is a sequence
                                # of (a) non-quote characters ([^"]) or
                                # (b) escaped quote characters (\\\\").
    

    Some other notes:

    1. preg_split uses the regular expression to match token at which the string should be split. That's not what you want in this case; you want to return the parts of the string that was matched by the regular expression. For this, you should use preg_match (or if, like here, you want a pattern to match multiple times), preg_match_all.
    2. Also consider the PREG_SET_ORDER flag for preg_match_all. This flag causes the $matches result to contain one row for each label from output message, which makes the data structure easy to process. Try and see what happens if you leave it out.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制