<?php
require_once "spyc.php";
$content = @file_get_contents("./actInit.yaml");
$yaml = Spyc::YAMLLoad($content);
print_r( $yaml );
?>
require_once "spyc.php";
$content = @file_get_contents("./actInit.yaml");
$yaml = Spyc::YAMLLoad($content);
print_r( $yaml );
?>
house:
family: { name: Doe, parents: [John, Jane], children: [Paul, Mark, Simone] }
address: { number: 34, street: Main Street, city: Nowheretown, zipcode: 12345 }
Array
(
[house] => Array
(
[family] => Array
(
[name] => Doe
[parents] => Array
(
[0] => John
[1] => Jane
)
[children] => Array
(
[0] => Paul
[1] => Mark
[2] => Simone
)
)
[address] => Array
(
[number] => 34
[street] => Main Street
[city] => Nowheretown
[zipcode] => 12345
)
)
)
family: { name: Doe, parents: [John, Jane], children: [Paul, Mark, Simone] }
address: { number: 34, street: Main Street, city: Nowheretown, zipcode: 12345 }
Array
(
[house] => Array
(
[family] => Array
(
[name] => Doe
[parents] => Array
(
[0] => John
[1] => Jane
)
[children] => Array
(
[0] => Paul
[1] => Mark
[2] => Simone
)
)
[address] => Array
(
[number] => 34
[street] => Main Street
[city] => Nowheretown
[zipcode] => 12345
)
)
)
YAML
来自YAML官方网站 (http://www.yaml.org/) 的定义: YAML是一种直观的能够被电脑识别的的数据数据序列化格式,它并且容易被人类阅读,容易与脚本语言交互的。换种说法,YAML是一种非常简单的类似于XML的数据描述语言,语法比XML简单很多。他在描述可以被转化成数组或者hash的数据是非常有用,例如:
$house = array(
'family' => array(
'name' => 'Doe',
'parents' => array('John', 'Jane'),
'children' => array('Paul', 'Mark', 'Simone')
),
'address' => array(
'number' => 34,
'street' => 'Main Street',
'city' => 'Nowheretown',
'zipcode' => '12345'
)
);
解析这个YAML将会自动创建下面的PHP数组:
house:
family:
name: Doe
parents:
- John
- Jane
children:
- Paul
- Mark
- Simone
address:
number: 34
street: Main Street
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 }
YAML是"Yet Another Markup Language(另一种标记语言)"的缩写,读音"yamel",或者"雅梅尔"。这种格式大约是2001年出现的,目前为止已经有多种语言的YAML解析器。
提示 YAML格式的详细规格可以在YAML官方网站http://www.yaml.org/找到。
如你所见,写YAML要比XML快得多(不需要关闭标签或者引号),并且比'.ini'文件功能更强(ini文件不支持层次)。所以symfony选择YAML作为配置信息的首选格式。在本书你会看到很多YAML文件,不过它很直观你用不着更深入地研究YAML。
以下两种写法完全一样:
<?php
require_once "spyc.php";
$content = @file_get_contents("./actInit.yaml");
$yaml = Spyc::YAMLLoad($content);
print_r( $yaml );
?>
<?php
require_once "spyc.php";
$filename = "./actInit.yaml";
$yaml = Spyc::YAMLLoad($filename);
print_r( $yaml );
?>
require_once "spyc.php";
$content = @file_get_contents("./actInit.yaml");
$yaml = Spyc::YAMLLoad($content);
print_r( $yaml );
?>
<?php
require_once "spyc.php";
$filename = "./actInit.yaml";
$yaml = Spyc::YAMLLoad($filename);
print_r( $yaml );
?>
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/2749/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2010-3-1 14:17
评论列表