将格式化的分隔文本行转换为关联数组
我有一串采用以下冒号分隔格式的键->值对:
MIME-Version: 1.0
From: "Tim Lincecum"
Reply-To: "Tim Lincecum"
Return-path: "Tim Lincecum"
Content-Type: text/html; charset=iso-8859-1
Subject: Giants Win World Series!
如何获取一个关联数组,例如 arr['From'] = "Tim Lincecum",
等?
我知道有 explode()
函数,但我看到的唯一分隔符(冒号)位于键和值的中间,而不是在每对之间。我该如何处理这个问题?
I have a string of key->value pairs in the following colon-separated format:
MIME-Version: 1.0
From: "Tim Lincecum"
Reply-To: "Tim Lincecum"
Return-path: "Tim Lincecum"
Content-Type: text/html; charset=iso-8859-1
Subject: Giants Win World Series!
How do I get an associative array such that arr['From'] = "Tim Lincecum",
etc.?
I know there's the explode()
function, but the only delimiter I see (colon) is in the middle of a key and a value rather than between each pair. How can I approach this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您始终可以使用正则表达式:)
PHP
输出
在 IDEone 上查看。
You could always use regex :)
PHP
Output
See it on IDEone.
$string
是您从数据库获取的值。$string
is the value you're getting from the database.因为我在评论中做了很好的猜测 - 我想我需要在这里重复它作为答案:
参数之间有换行符,因此
您可以将其拆分为名称-值对,并用冒号分隔。
Since I did a good guess in comments - I think i need to repeat it here as an answer:
There is a newlines between parameters, so with
you can split it into the name-value pairs, separated with colon.
在解析本机 PHP 函数/库不处理的文本格式时,避免使用正则表达式方法将需要在一个分隔符上爆炸,然后循环这些结果并在另一个分隔符上爆炸。
为了简单和控制,只需使用
preg_match_all()
,然后使用array_column()
从捕获的子字符串的有效负载形成关联结构。代码:(演示)
m
模式修饰符更改^ 的含义
从“字符串的开头”到“行的开头”。如果您想删除某些值上出现的双引号,请执行以下模式调整:演示
Avoiding a regular expression approach while parsing a text format that is not handled by a native PHP function/library will require exploding on one delimiter, then looping those results and exploding on another delimiter.
For simplicity and control, just use
preg_match_all()
, then usearray_column()
to form the associative structure from the payload of captured substrings.Code: (Demo)
The
m
pattern modifier changes the meaning of^
from "start of the string" to "start of a line".If you'd like to remove the double quote wrapping that appears on some of the values, here is the pattern adjustment: Demo