解析具有父数据和缩进子数据的多行字符串并创建多维数组
我正在尝试在 PHP 中解析字符串:
-father_name "John" -father_weight 44.50
-kid >name "Marko" >age 12
-kid >name "Sevda" >age 17
-kid >name "Nathan" >age 19
有两种主要形式:
- 属性(例如 -father、-weight、-kid)
- 子属性(例如 >name、>age)
注意: 属性不是固定的,也不总是用单个空格分隔
,它们的值有两种类型:
- 字符串(如“Marko”)
- Int 或 Decimal(如 12.00)
输出将是:
[
'father_name' => 'John',
'father_weight' => '44.50',
'kid' => [
['name' => "Marko", 'age' => 12],
['name' => "Sevda", 'age' => 17],
['name' => "Nathan", 'age' => 19]
]
]
它应该返回 >表单(属性和子属性)和值分开。
如何巧妙地解析 PHP 中的这一行?
最后说明:我为此找到的解决方案:YAML。
I am trying to parse a string in PHP:
-father_name "John" -father_weight 44.50
-kid >name "Marko" >age 12
-kid >name "Sevda" >age 17
-kid >name "Nathan" >age 19
There are two main FORMS:
- Attributes (such as -father, -weight, -kid)
- Sub-Attributes (such as >name, >age)
Note: Attributes are NOT FIXED and NOT ALWAYS SEPERATED BY single space
And their VALUES have two types:
- String (like "Marko")
- Int or Decimal (like 12.00)
OUTPUT would be:
[
'father_name' => 'John',
'father_weight' => '44.50',
'kid' => [
['name' => "Marko", 'age' => 12],
['name' => "Sevda", 'age' => 17],
['name' => "Nathan", 'age' => 19]
]
]
It should return FORMS (attrs and sub-attrs) and VALUES SEPARATELY.
How can I parse this line in PHP cleverly?
Last Note: Solution I found for this: YAML.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用以下方法:
使用:
返回:
并使用:
返回:
Marko
注意:您可以指定更多分隔符数组项,每个属性级别对应一个项。
希望这有帮助。
Try with this:
Using:
Returns:
And using:
Returns:
Marko
NOTE: You can specify more separator array items, an item for each attribute level you have.
Hope this helps.