Does anyone know of a good YAML Parser for PHP? If so, what are the pros and cons of this library?
-
1try "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
-
2considering 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 Answers
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:
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

- 83,810
- 28
- 209
- 234
-
1There 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
-
3I'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
-
12019 update for PHP: "spyc" has so many problems, while "sfYaml" works fine. – ViliusL Nov 29 '19 at 15:05
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);
-
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
-
1Outdated and lots of parse yaml files incorrectly (some data are lost). – ViliusL Nov 29 '19 at 14:58
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.

- 743
- 4
- 11
Symfony2 has a YAML component which supports most of the YAML 1.2 spec

- 189
- 1
- 3
-
This is a good answer because the Symfony2 YAML parser is not outdated. `sfYaml` e.g. is. – ferdynator Jan 01 '14 at 20:06
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:

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

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

- 48,968
- 59
- 172
- 213