Does anyone know of a good YAML Parser for PHP? If so, what are the pros and cons of this library?
Does anyone know of a good YAML Parser for PHP? If so, what are the pros and cons of this library?
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);
The symfony framework makes very heavy use of YAML, this blog post by Grégoire Hubert demonstrates using their YAML library in a non-symfony project.
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:
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.