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 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型