(PHP)Parse命令

时间:2011-08-12 13:21:25

标签: php arrays parsing

我想得到命令标签的值(GET,FROM,IN等)我的命令是:

// My command
$_cmd = 'GET a, b FROM p IN a and c="I am from Sarajevo" or d>1 ';

// My parser
if(preg_match_all('/(GET|FROM|IN)\s+([^\s]+)/si',$_cmd, $m))
    $cmd = array_combine($m[1], $m[2]);

输出:

Array
(
  [GET] => a,
  [FROM] => p
  [IN] => a
  [from] => Sarajevo"
)

我正在寻找这个输出:

Array
(
  [GET] => a, b
  [FROM] => p
  [IN] => a and c="I am from Sarajevo" or d>1
)

如您所见,问题在于字符串中的空格重复的命令标记(例如from)。那我怎么解析这个命令呢?

5 个答案:

答案 0 :(得分:8)

使用单个正则表达式无法轻松解析它。 (这是可行的,但并不简单。)

你应该使用一个简单的标记化器,其中正则表达式再次成为一个有用的工具:

  preg_match_all('/\w+|".*?"|\W/', $_cmd = 'GET a, b FROM p IN a and c="I am from Sarajevo" or d>1 ', $list);

这为您提供了一个简单的列表,您只需找到您感兴趣的子句,然后重新合并后续的令牌(尽管我对您的用例感到困惑):

[0] => Array
    (
        [0] => GET
        [1] => a
        [2] => ,
        [3] => b
        [4] => FROM
        [5] => p
        [6] => IN
        [7] => a
        [8] => and
        [9] => c
        [10] => =
        [11] => "I am from Sarajevo"
        [12] => or
        [13] => d
        [14] => >
        [15] => 1
    )

答案 1 :(得分:3)

if( preg_match_all('/(GET|FROM|IN)(.(?!(GET|FROM|IN)))+\s*/si',$_cmd, $m))

这意味着 - 找到关键字之后的任何字符,后面没有GET,FROM或IN后面的空格

答案 2 :(得分:1)

您需要为此开发脚本语言。正则表达式不适用于这些目的。

答案 3 :(得分:1)

您可以在分隔符i后删除不区分大小写的/。并且还要确保关键字后面至少有一个空格。

答案 4 :(得分:1)

$_cmd = 'GET a, b FROM p IN a and c="I am from Sarajevo" or d>1 ';
$tpar = preg_split('/\s+(GET|FROM|IN)\s+/i', ' '.$_cmd.' ', -1, PREG_SPLIT_DELIM_CAPTURE);
array_walk($tpar, 'trim');

print_r($tpar);

// gives:
array(
  [0] => GET
  [1] => a, b
  [2] => FROM
  [3] => p
  [4] => IN
  [5] => a and c="I am from Sarajevo" or d>1
)
// the rest is straight forward