什么是YAML
YAML是”Yet Another Markup Language(另一种标记语言)”的缩写,读音”yamel”,或”雅梅尔”。这种格式大约是2001年出现的,现在为止已有多种语言的YAML解析器。
提示 YAML格式的周详规格能够在YAML官方网站http://www.yaml.org/找到。
如您所见,写YAML要比XML快得多(无需关闭标签或引号),并且比’.ini’文档功能更强(ini文档不支持层次)。所以symfony选择YAML作为配置信息的最好选择格式。
(above from baidu Baike)
$house=array(
’family’=>array(
’name’=>’Doe’,
’parents’=>array(‘John’,’Jane’),
’children’=>array(‘Paul’,’Mark’,’Simone’)
),
’address’=>array(
’number’=>34,
’street’=>’MainStreet’,
’city’=>’Nowheretown’,
’zipcode’=>’12345′
)
);
解析这个YAML将会自动创建下面的PHP数组:
house:
family:
name:Doe
parents:
-John
-Jane
children:
-Paul
-Mark
-Simone
address:
number:34
street:MainStreet
city:Nowheretown
zipcode:12345
在YAML里面,结构通过缩进来表示,连续的项目通过减号”-”来表示,map结构里面的key/value对用冒号”:”来分隔。YAML也有用来描述好几行相同结构的数据的缩写语法,数组用’[]‘包括起来,hash用’{}’来包括。因此,前面的这个YAML能够缩写成这样:
house:
family: { name: Doe, parents: [John, Jane], children: [Paul, Mark, Simone] }
address: { number: 34, street: Main Street, city: Nowheretown, zipcode: 12345 }
关于php解析YAML:
spyc:
Spyc is a YAML loader/dumper written in PHP. Given a YAML document,Spyc will return an array which you can use however you see fit. Givenan array, Spyc will return a string which contains a YAML documentbuilt from your data.
例子:
$yaml = Spyc::YAMLLoad(‘spyc.yaml‘); // 将数组转换成YAML文件
$array['name'] = ‘wx‘;
$array['site'] = ‘loopx.cn‘;
$yaml = Spyc::YAMLDump($array);
//加载变量
$yamlarr=Spyc::YAMLLoad($yaml);
