首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在php中解析name = value对?什么是至少令人满意的方法?

如何在php中解析name = value对?什么是至少令人满意的方法?
EN

Stack Overflow用户
提问于 2011-11-18 16:25:39
回答 2查看 574关注 0票数 0

令人满意的是,不会进入耗费大量时间的函数中。

数据的格式:它基本上类似于HTML标签参数,但不完全是。

代码语言:javascript
复制
<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中有了解析功能?

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-18 17:19:49

尝试如下所示:

代码语言:javascript
复制
$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";
}

它将打印以下内容:

代码语言:javascript
复制
===============================================
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```

使用和维护,风险自负!

票数 1
EN

Stack Overflow用户

发布于 2011-11-18 16:38:40

XML允许名称中包含空格的示例

代码语言:javascript
复制
<item>
 <name>name with spaces</name>
 <value>
   value with spaces
   and linebreaks
 </value>
</item>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8179603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档