如何解析名称=值对在PHP ?什么是至少令人满意的方法


how to parse name = value pairs in php ? what is at least satisfactory approach?

令人满意的是,不要进入耗时的函数。

数据格式:它基本上与HTML标记参数相似,但又不完全相同。

<name1=value1 name2=value2 [here can be any whitespace characters]
33=55 "name with spaces and ="="value with spaces and '" '"title"'="can't" "in
names"="and
values can be tabs and new lines"
double"and_single'quotes=are_allowed_in_names_and_values_if_there_is_no_whitespace_in_it
"escaping is ""sql-like"""='the same with ''single quotes'''
`third option`=`are "backticks" that also can be ``escaped```>

这个格式被调用了吗?我自己"发明"了它,但我非常怀疑以前没有人提出过这个想法,也许它(或非常类似的格式,例如斜杠转义)已经在php中有解析函数了?

如果没有,如何写自己的,使用正则表达式,字符串操作,某种类型的缓冲区,累加器?我自己从来没有写过任何解析器

试试这样:

$in = <<<BLOCK
name1=value1 
name2=value2 [here can be any whitespace characters]
33=55 
"name with spaces and ="="value with spaces and '" 
'"title"'="can't" 
"in names"="and
  values can be tabs and new lines"
double"and_single'quotes=are_allowed_in_names_and_values_if_there_is_no_whitespace_in_it
"escaping is ""sql-like"""='the same with ''single quotes'''
`third option`=`are "backticks" that also can be ``escaped```
BLOCK;
$string = "(?:                    # open non-capture group
             '"(?:'"'"|[^'"])+'"  # match a double quoted string
           | '(?:''|[^'])+'       # OR match a single quoted string
           | `(?:``|[^`])+`       # OR match a back-ticked string
           )                      # close non-capture group
          ";
$key    = "(                      # start capture group 1
             $string              # match any string
           | [^='s]+              # OR match one or more chars other than '=' and space-chars
           )                      # end capture group 1
          ";
$value  = "(                      # start capture group 2
             $string              # match any string
           | [^'r'n]+             # OR match one or more chars other than '=' and space-chars
           )                      # end capture group 2
          ";
$regex = "/$key=$value/x"; // combine the patterns
preg_match_all($regex, $in, $matches);
$key_value_pairs = sizeof($matches[0]);
for($index = 0; $index < $key_value_pairs; $index++) {
  echo "==============================================='n" . 
       "KEY:'n" . $matches[1][$index] . "'n" .
       "VALUE:'n" . $matches[2][$index] . "'n";
}

将打印以下内容:

===============================================
KEY:
name1
VALUE:
value1 
===============================================
KEY:
name2
VALUE:
value2 [here can be any whitespace characters]
===============================================
KEY:
33
VALUE:
55 
===============================================
KEY:
"name with spaces and ="
VALUE:
"value with spaces and '"
===============================================
KEY:
'"title"'
VALUE:
"can't"
===============================================
KEY:
"in names"
VALUE:
"and
  values can be tabs and new lines"
===============================================
KEY:
double"and_single'quotes
VALUE:
are_allowed_in_names_and_values_if_there_is_no_whitespace_in_it
===============================================
KEY:
"escaping is ""sql-like"""
VALUE:
'the same with ''single quotes'''
===============================================
KEY:
`third option`
VALUE:
`are "backticks" that also can be ``escaped```

使用和维护,风险自负!

允许在名称中使用空格的XML示例

<item>
 <name>name with spaces</name>
 <value>
   value with spaces
   and linebreaks
 </value>
</item>