PHP - yaml_parse() 函数


yaml_parse() 函数可以解析 YAML 流。更多的相关函数请看:PHP YAML Data Serialization 函数

语法


mixed yaml_parse( string $input [, int $pos = 0 [, int &$ndocs [, array $callbacks = null ]]] )

yaml_parse() 函数可以将全部或部分 YAML 文档流转换为 PHP 变量。

yaml_parse() 函数可以返回一个在输入中以适当的 PHP 类型编码的值,或者在失败时返回 false。

如果 pos 为 -1,则可以返回一个数组,其中包含一个流中找到每个文档的条目。

示例


<?php
   $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: 下午晚些时候最好。备用联系人是李科,电话:020-4338。
   ...
   EOD;
   $parsed = yaml_parse($yaml);
   var_dump($parsed);
?>