This extension implements the » YAML Ain't Markup Language (YAML) data serialization standard. Parsing and emiting are handled by the » LibYAML library.
This extension requires the » LibYAML C library version 0.1.0 or higher to be installed.
This » PECL extension is not bundled with PHP.
Information for installing this PECL extension may be found in the manual chapter titled Installation of PECL extensions. Additional information such as new releases, downloads, source files, maintainer information, and a CHANGELOG, can be located here: » http://code.google.com/p/php-yaml/.
A DLL for this PECL extension is currently unavailable. See also the building on Windows section.
The behaviour of these functions is affected by settings in php.ini.
| Name | Default | Changeable | Changelog |
|---|---|---|---|
| yaml.decode_binary | 0 | PHP_INI_ALL | |
| yaml.decode_timestamp | 0 | php_ini_all | |
| yaml.output_canonical | 0 | php_ini_all | |
| yaml.output_indent | 2 | php_ini_all | |
| yaml.output_width | 80 | php_ini_all |
Here's a short explanation of the configuration directives.
Off by default, but can be set to on to cause base64 binary encoded entities which have the explicit tag "tag:yaml.org,2002:binary" to be decoded.
Controls the decoding of both implicit and explict "tag:yaml.org,2002:timestamp" scalars in the YAML document stream. The default setting of 0 will not apply any decoding. A setting of 1 will use strtotime to parse the timestamp value as a Unix timestamp. A setting of 2 will use date_create to parse the timestamp value as DateTime object.
Off by default, but can be set to on to cause canonical form output.
Number of spaces to indent sections. Value should be between 1 and 10.
Set the preferred line width. -1 means unlimited.
The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.
Example #1 Yaml Example
<?php
$addr = array(
"given" => "Chris",
"family"=> "Dumars",
"address"=> array(
"lines"=> "458 Walkman Dr.
Suite #292",
"city"=> "Royal Oak",
"state"=> "MI",
"postal"=> 48046,
),
);
$invoice = array (
"invoice"=> 34843,
"date"=> "2001-01-23",
"bill-to"=> $addr,
"ship-to"=> $addr,
"product"=> array(
array(
"sku"=> "BL394D",
"quantity"=> 4,
"description"=> "Basketball",
"price"=> 450,
),
array(
"sku"=> "BL4438H",
"quantity"=> 1,
"description"=> "Super Hoop",
"price"=> 2392,
),
),
"tax"=> 251.42,
"total"=> 4443.52,
"comments"=> "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.",
);
// generate a YAML representation of the invoice
$yaml = yaml_emit($invoice);
var_dump($yaml);
// convert the YAML back into a PHP variable
$parsed = yaml_parse($yaml);
// check that roundtrip conversion produced an equivalent structure
var_dump($parsed == $invoice);
?>
The above example will output something similar to:
string(631) "---
invoice: 34843
date: "2001-01-23"
bill-to:
given: Chris
family: Dumars
address:
lines: |-
458 Walkman Dr.
Suite #292
city: Royal Oak
state: MI
postal: 48046
ship-to:
given: Chris
family: Dumars
address:
lines: |-
458 Walkman Dr.
Suite #292
city: Royal Oak
state: MI
postal: 48046
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.
...
"
bool(true)
Send the YAML representation of a value to a file
Generate a YAML representation of the provided data in the filename .
Path to the file.
The data being encoded. Can be any type except a resource .
Output character encoding chosen from YAML_ANY_ENCODING, YAML_UTF8_ENCODING, YAML_UTF16LE_ENCODING, YAML_UTF16BE_ENCODING. Defaults to YAML_ANY_ENCODING.
Output linebreak style chosen from YAML_ANY_BREAK, YAML_CR_BREAK, YAML_LN_BREAK, YAML_CRLN_BREAK. Defaults to YAML_ANY_BREAK.
Returns TRUE on success.
Returns the YAML representation of a value
Generate a YAML representation of the provided data .
The data being encoded. Can be any type except a resource .
Output character encoding chosen from YAML_ANY_ENCODING, YAML_UTF8_ENCODING, YAML_UTF16LE_ENCODING, YAML_UTF16BE_ENCODING. Defaults to YAML_ANY_ENCODING.
Output linebreak style chosen from YAML_ANY_BREAK, YAML_CR_BREAK, YAML_LN_BREAK, YAML_CRLN_BREAK. Defaults to YAML_ANY_BREAK.
Returns a YAML encoded string on success.
Example #1 yaml_emit example
<?php
/* ... */
?>
The above example will output something similar to:
...
Parse a YAML stream from a file
Convert all or part of a YAML document stream read from a file to a PHP variable.
Path to the file.
Document to extract from stream (-1 for all documents, 0 for first document, ...).
If ndocs is provided, then it is filled with the number of documents found in stream.
Content handlers for YAML nodes. Associative array of YAML tag => callback mappings.
Returns the value encoded in input in appropriate PHP type. NULL is returned if the input cannot be decoded. If pos is -1 an array will be returned with one entry for each document found in the stream.
Parse a Yaml stream from a URL
Convert all or part of a YAML document stream read from a URL to a PHP variable.
url should be of the form "scheme://...". PHP will search for a protocol handler (also known as a wrapper) for that scheme. If no wrappers for that protocol are registered, PHP will emit a notice to help you track potential problems in your script and then continue as though filename specifies a regular file.
Document to extract from stream (-1 for all documents, 0 for first document, ...).
If ndocs is provided, then it is filled with the number of documents found in stream.
Content handlers for YAML nodes. Associative array of YAML tag => callback mappings.
Returns the value encoded in input in appropriate PHP type. NULL is returned if the input cannot be decoded. If pos is -1 an array will be returned with one entry for each document found in the stream.
Parse a YAML stream
Convert all or part of a YAML document stream to a PHP variable.
The string to parse as a YAML document stream.
Document to extract from stream (-1 for all documents, 0 for first document, ...).
If ndocs is provided, then it is filled with the number of documents found in stream.
Content handlers for YAML nodes. Associative array of YAML tag => callback mappings.
Returns the value encoded in input in appropriate PHP type. NULL is returned if the input cannot be decoded. If pos is -1 an array will be returned with one entry for each document found in the stream.
Example #1 yaml_parse example
<?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: Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.
...
EOD;
$parsed = yaml_parse($yaml);
var_dump($parsed);
?>
The above example will output something similar to:
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."
}