[ Поиск ] - [ Пользователи ] - [ Календарь ]
Полная Версия: парсер yaml
Страницы: 1, 2
pamparam
вот пример кода который нужно спарсить..

$yaml = <<<EOD
---
invoice: 34843
date: "2001-01-23"
bill-to: &id001
given: Chris
family: Dumars
address:
lines: |-
458 Walkman Dr.
Suite #292
city: Royal Oak
state: MI
postal: 48046
ship-to: *id001
product:
- sku: BL394D
quantity: 4
description: Basketball
price: 450
- sku: BL4438H
quantity: 1
description: Super Hoop
price: 2392
tax: 251.420000
total: 4443.520000
comments: Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.
...

EOD;



вот то что ожидается

array(8) {
["invoice"]=>
int(34843)
[
"date"]=>
string(10) "2001-01-23"
["bill-to"]=>
&
array(3) {
["given"]=>
string(5) "Chris"
["family"]=>
string(6) "Dumars"
["address"]=>
array(4) {
["lines"]=>
string(34) "458 Walkman Dr.
Suite #292"

["city"]=>
string(9) "Royal Oak"
["state"]=>
string(2) "MI"
["postal"]=>
int(48046)
}
}

["ship-to"]=>
&
array(3) {
["given"]=>
string(5) "Chris"
["family"]=>
string(6) "Dumars"
["address"]=>
array(4) {
["lines"]=>
string(34) "458 Walkman Dr.
Suite #292"

["city"]=>
string(9) "Royal Oak"
["state"]=>
string(2) "MI"
["postal"]=>
int(48046)
}
}

["product"]=>
array(2) {
[0]=>
array(4) {
["sku"]=>
string(6) "BL394D"
["quantity"]=>
int(4)
[
"description"]=>
string(10) "Basketball"
["price"]=>
int(450)
}
[1]=>
array(4) {
["sku"]=>
string(7) "BL4438H"
["quantity"]=>
int(1)
[
"description"]=>
string(10) "Super Hoop"
["price"]=>
int(2392)
}
}

["tax"]=>
float(251.42)
[
"total"]=>
float(4443.52)
[
"comments"]=>
string(68) "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338."
}


так делаю одномерный

$pattern = '#[\w-]+: [a-z0-9"-.]+#';
preg_match_all($pattern, $yaml, $matches, PREG_OFFSET_CAPTURE);

foreach($matches as $val){
foreach($val as $k){
$array = explode(":",$k[0]);
$yaml_dec[$array[0]]= $array[1];
}
}



одномерный могу сделать а многомерный не могу понять как
подскажите куда смотреть?
Быстрый ответ:

 Графические смайлики |  Показывать подпись
Здесь расположена полная версия этой страницы.
Invision Power Board © 2001-2024 Invision Power Services, Inc.