Does anyone know of a good YAML Parser for PHP? If so, what are the pros and cons of this library?
Update: Starting a bounty to get fresh input. What's the status of YAML parsers in 2010? Any new developments?
Does anyone know of a good YAML Parser for PHP? If so, what are the pros and cons of this library?
Update: Starting a bounty to get fresh input. What's the status of YAML parsers in 2010? Any new developments?
I'd suggest the process followed in this article http://devzone.zend.com/article/2585-using-yaml-with-php-and-pecl
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 want to just a simple yaml parser written in PHP itself, I could provide simple free script you may want to adjust if you want.
i'm using it quite extensively on Amy Editor for storing bundle definitions. it's easy and with Ruby-like API (Simple YAML::load(..) :).
Spyc: http://code.google.com/p/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.
Loading information into PHP is as simple as:
include_once('spyc.php');
$data = Spyc::YAMLLoad($myfile);
Where $myfile is a string or filename (it will attempt to detect if you've passed a filename, and process it.
Generating a YAML string is as simple as:
$yaml_str = Spyc::YAMLDump($some_array);
Where $some_array is an array of data to be output.
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.
If you need to test your YAML quickly, I built: http://yaml-online-parser.appspot.com/ . It helps me write YAML, especially while just learning.
Well, there's nothing new beyond what all the other answers provide. Here's a summary of the state of YAML in PHP:
Pure PHP implementations:
At the time of this writing, the latest versions release dates for the aforementioned libraries and the versions of the YAML spec they support are:
php-yaml 0.6.3 2010-02-09 YAML 1.1
syck 0.9.3 2008-11-18 YAML 1.0
sfYaml 1.0.3 2010-06-29 YAML 1.1, partial 1.2
spyc 0.4.5 2009-09-20 YAML 1.1