108

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

apaderno
  • 28,547
  • 16
  • 75
  • 90
sgibbons
  • 3,620
  • 11
  • 36
  • 31
  • 1
    try "spyc" lib http://spyc.sourceforge.net/ – Shreef Nov 16 '08 at 22:34
  • 18
    +1 although non-constructive, this page was extremely helpful to me – Erick Robertson Mar 06 '14 at 16:53
  • 2
    considering the quality of the highest-voted answer on this question, it is astonishing that this question was closed as "not constructive". – dreftymac Jun 08 '16 at 02:13
  • I just released my parser of BabyYaml, which is a simpler cousin of Yaml, if you are interested check it out here: https://github.com/lingtalfi/BabyYaml – ling Dec 28 '16 at 22:28
  • I have to agree with ErickRobertson and dreftymac this literally answered my question well, and closing it has prevented further possible good answers. – Pharaoh Tools Apr 04 '19 at 19:04

7 Answers7

149

Last updated: July 26th, 2017

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: YAML parser without dependencies

At the time of this writing, the latest versions release dates for the aforementioned libraries and the versions of the YAML spec (1.2 is the latest 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 
NullUserException
  • 83,810
  • 28
  • 209
  • 234
  • 1
    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 Sep 11 '10 at 16:47
  • 3
    I've been using sfYaml for about two years, and on a performance test I realized that sfYaml::parse() took the most of the processing time. I replaced it with php-yaml and the application is now 70% faster! That's a huge difference. – Attila Fulop Feb 14 '13 at 13:25
  • 1
    2019 update for PHP: "spyc" has so many problems, while "sfYaml" works fine. – ViliusL Nov 29 '19 at 15:05
49

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);
Dennis
  • 56,821
  • 26
  • 143
  • 139
drowe
  • 2,312
  • 18
  • 14
  • 1
    +1 I just wanted a quick and easy way to use YAML in an import script. My application does not need any support beyond imports (and probably someday exports). I found that spyc, listed as "yet another YAML parser" was a 1-click solution. I dropped the one script into my scripts folder, included it, and called one method. It doesn't get easier than that, and now I'm going back to work - roadblock removed. – Erick Robertson Mar 06 '14 at 16:59
  • 1
    Outdated and lots of parse yaml files incorrectly (some data are lost). – ViliusL Nov 29 '19 at 14:58
16

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
  • 743
  • 4
  • 11
7

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

https://github.com/symfony/Yaml

Shane
  • 189
  • 1
  • 3
3

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
  • 1,781
  • 1
  • 22
  • 31
2

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
  • 49,108
  • 32
  • 120
  • 164
2

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
  • 48,968
  • 59
  • 172
  • 213