将每行具有两个分隔符的多行字符串解析为关联行数组
我发现了一些将数据分解为数组的帖子,但我的有点具体,因为数据有 2 个部分
,我有这个
title=Title1|link=Link1
title=Title2|link=Link2
,结果我需要的是这个
Array
(
[0] => Array
(
[title] => Title1
[link] => Link1
)
[1] => Array
(
[title] => Title2
[link] => Link2
)
)
数据来自 texarea,用 \n 分隔,所以你看到的数据是实际数据。
I found a few posts to explode data as array, but mine is bit specific because the data has 2 parts
I have this
title=Title1|link=Link1
title=Title2|link=Link2
and result I need is this
Array
(
[0] => Array
(
[title] => Title1
[link] => Link1
)
[1] => Array
(
[title] => Title2
[link] => Link2
)
)
data is coming from texarea separated by \n so the data you see is actual data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
print_r($reg)
生成我相信您可以轻松修改它以适合您所需的架构。
print_r($reg)
producesI'm sure you can easily modify that to fit your required schema.
当您实际上不需要正则表达式时,我更喜欢
explode()
而不是preg_match()
。不要忘记添加代码来处理与您期望的模式不匹配的行。输入验证很重要。
I'm more a fan of
explode()
thanpreg_match()
when you don't actually need regular expressions.Don't forget to add code to handle lines that don't match the pattern you expect. Input validation is important.
也许像这样,它有点冗长,但您应该能够使用它作为更优雅的东西的基础。
Maybe something like this, it's a bit verbose but you should be able to use it as the basis for something more elegant.