[ACCEPTED]-PHP YAML Parsers-yaml

Accepted answer
Score: 146

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:

    • sfYaml: Symfony's YAML component. You can see its authors' motivations here. He wanted something that was "easy to use, fast, unit tested and had clear error messages."
    • spyc: YAML parser without dependencies

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 
Score: 50

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);
Score: 16

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.

Score: 7

Symfony2 has a YAML component which supports 1 most of the YAML 1.2 spec


Score: 3

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:

  • yaml - a wrapper for the LibYAML YAML 1.1 parser library
  • syck - a wrapper for the Syck YAML 1.0 parser library
Score: 2

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.

Score: 2

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