PHP YAML Parsers

Viewed 59715

Does anyone know of a good YAML Parser for PHP? If so, what are the pros and cons of this library?

7 Answers

Spyc: https://github.com/mustangostang/spyc

Pure PHP implementation, so you don't need to make any modifications to the server for installation. If speed is of dire concern, it might not be the ideal solution, but if you're using YAML for configurations or relatively low-volume use, it is a fantastic solution.

Given a YAML document, Spyc will return an array that you can use however you see fit.

require_once "spyc.php";
$data = Spyc::YAMLLoad($myfile);

Given an array, Spyc will return a string which contains a YAML document built from your data.

$yaml_str = Spyc::YAMLDump($myarray);

If you're using a lot of YAML in your project you may find that the pure PHP libraries like spyc or Symfony YAML are not fast enough. There are at least two PHP bindings for C YAML parsers:

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

Try sfYaml, it is the best I know.

Symfony and Doctrine ORM are using this one.

To get it, you may Download Doctrine 1.2 and extract sfYaml from vendor directory.

Let us know if it suits your needs.

Related