[ACCEPTED]-PHP YAML Parsers-yaml
Last updated: July 26th, 2017
Here's a summary of the 5 state of YAML in PHP:
- Wrappers to C libraries: You'll probably want these if you need sheer speed:
- php-yaml: Wrapper for LibYAML. Available as a PECL extension; it is also the one on PHP's docs.
- syck: Binding to syck; also available as a PECL extension. (dated, see below)
Pure PHP implementations:
At 4 the time of this writing, the latest versions 3 release dates for the aforementioned libraries 2 and the versions of the YAML spec (1.2 is the latest 1 version) they support are:
php-yaml 1.3.0 2016-09-24 YAML 1.1 [PHP 5]
php-yaml 2.0.0 2016-09-24 YAML 1.1 [PHP 7]
syck 0.9.3 2008-11-18 YAML 1.0
sfYaml 3.3.5 2017-06-15 YAML 1.1, most of 1.2
spyc 0.6.2 2017-02-24 YAML 1.1
Spyc: https://github.com/mustangostang/spyc
Pure PHP implementation, so you don't 10 need to make any modifications to the server 9 for installation. If speed is of dire concern, it 8 might not be the ideal solution, but if 7 you're using YAML for configurations or 6 relatively low-volume use, it is a fantastic 5 solution.
Given a YAML document, Spyc will 4 return an array that you can use however 3 you see fit.
require_once "spyc.php";
$data = Spyc::YAMLLoad($myfile);
Given an array, Spyc will return 2 a string which contains a YAML document 1 built from your data.
$yaml_str = Spyc::YAMLDump($myarray);
The symfony framework makes very heavy use of YAML, this 2 blog post by Grégoire Hubert demonstrates using their YAML library in 1 a non-symfony project.
Symfony2 has a YAML component which supports 1 most of the YAML 1.2 spec
If you're using a lot of YAML in your project 4 you may find that the pure PHP libraries 3 like spyc or Symfony YAML are not fast enough. There 2 are at least two PHP bindings for C YAML 1 parsers:
Try sfYaml, it is the best I know.
Symfony and 3 Doctrine ORM are using this one.
To get it, you 2 may Download Doctrine 1.2 and extract sfYaml
from vendor
directory.
Let us 1 know if it suits your needs.
If you need to test your YAML quickly, I 2 built: http://yaml-online-parser.appspot.com/ . It helps me write YAML, especially 1 while just learning.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.