douaipi3965
2015-06-21 10:14 阅读 69
已采纳

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

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 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.
    点赞 2 评论 复制链接分享
  • duanniu3385 duanniu3385 2015-06-21 11:43

    It's because of the greedy dot that eats up your string to the last ". Make it lazy, would do like that:

    if(preg_match_all('/(\w+)="(.*?)(?<!\\\)"/s', $str, $out))
      print_r(array_combine($out[1], $out[2]));
    

    \w is a short for [a-zA-Z0-9_]. The lookbehind (?<!\\\) to eat up escaped quotes (see regex101).

    Used s flag for making the dot match newline. Test at eval.in, outputs to:

    Array ( [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. )

    点赞 8 评论 复制链接分享

相关推荐