在PHP中解析属性/值列表

时间:2009-10-22 07:47:51

标签: php regex parsing

给定一个包含属性/值对的字符串,例如

attr1="some text" attr2 = "some other text" attr3= "some weird !@'#$\"=+ text"

目标是解析它并输出一个关联数组,在这种情况下:

array('attr1' => 'some text',
      'attr2' => 'some other text',
      'attr3' => 'some weird !@\'#$\"=+ text')

请注意等号周围的不一致间距,输入中的转义双引号以及输出中的转义单引号。

2 个答案:

答案 0 :(得分:6)

尝试这样的事情:

$text = "attr1=\"some text\" attr2 = \"some other text\" attr3= \"some weird !@'#$\\\"=+ text\"";
echo $text;
preg_match_all('/(\S+)\s*=\s*"((?:\\\\.|[^\\"])*)"/', $text, $matches, PREG_SET_ORDER);
print_r($matches);

产生:

attr1="some text" attr2 = "some other text" attr3= "some weird !@'#$\"=+ text"

Array
(
    [0] => Array
        (
            [0] => attr1="some text"
            [1] => attr1
            [2] => some text
        )

    [1] => Array
        (
            [0] => attr2 = "some other text"
            [1] => attr2
            [2] => some other text
        )

    [2] => Array
        (
            [0] => attr3= "some weird !@'#$\"=+ text"
            [1] => attr3
            [2] => some weird !@'#$\"=+ text
        )

)

简短的解释:

(\S+)               // match one or more characters other than white space characters
                    // > and store it in group 1
\s*=\s*             // match a '=' surrounded by zero or more white space characters 
"                   // match a double quote
(                   // open group 2
  (?:\\\\.|[^\\"])* //   match zero or more sub strings that are either a backslash
                    //   > followed by any character, or any character other than a
                    //   > backslash
)                   // close group 2
"                   // match a double quote

答案 1 :(得分:2)

编辑:如果值以attr4="something\\"

之类的反斜杠结尾,则此正则表达式失败

我不懂PHP,但由于正则表达式在任何语言中基本相同,所以我在ActionScript中就是这样做的:

var text:String = "attr1=\"some text\" attr2 = \"some other text\" attr3= \"some weird !@'#$\\\"=+ text\"";

var regex:RegExp = /\s*(\w+)\s*=\s*(?:"(.*?)(?<!\\)")\s*/g;

var result:Object;
while(result = regex.exec(text))
    trace(result[1] + " is " + result[2]);

我得到了以下内容:

  

attr1是一些文字
  attr2是其他一些文字
  attr3有些奇怪!@'#$ \“= + text