views:

5193

answers:

11
+20  Q: 

PHP YAML Parsers

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?

+2  A: 

I'd suggest the process followed in this article http://devzone.zend.com/article/2585-using-yaml-with-php-and-pecl

preinheimer
+8  A: 

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.

Dan Powley
A: 

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.

view/download here

i'm using it quite extensively on Amy Editor for storing bundle definitions. it's easy and with Ruby-like API (Simple YAML::load(..) :).

aprilchild
Downvoted for broken link.
artlung
A: 

try "spyc" lib http://spyc.sourceforge.net/

Shreef
+15  A: 

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.

drowe
A: 

http://components.symfony-project.org/yaml

+1  A: 

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
bd808
A: 

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.

takeshin
A: 

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.

Paul Tarjan
+9  A: 

Well, there's nothing new beyond what all the other answers provide. Here's a summary of the 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: Yet another YAML parser.

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
NullUserException
There are other lesser known libraries (like [this one](http://pear.horde.org/index.php?package=yaml), from the Horde project), but I didn't want to delve much into these.
NullUserException
Thanks for the research!
Pekka
A: 

Does anyone know if there is going to be a Pear port of any of these YAML parsers? Will any of the parsers Pear already has handle it?

Geekster