PHP YAML 解析器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/294355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP YAML Parsers
提问by sgibbons
Does anyone know of a good YAML Parser for PHP? If so, what are the pros and cons of this library?
有谁知道一个好的 PHP YAML 解析器?如果是这样,这个库的优缺点是什么?
回答by NullUserException
Last updated: July 26th, 2017
最后更新:2017 年 7 月 26 日
Here's a summary of the state of YAML in PHP:
以下是 PHP 中 YAML 状态的总结:
- 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:
- C 库的包装器:如果您需要绝对的速度,您可能会想要这些:
纯 PHP 实现:
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:
在撰写本文时,上述库的最新版本发布日期以及它们支持的YAML 规范版本(1.2 是最新版本)是:
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
回答by drowe
Spyc: https://github.com/mustangostang/spyc
间谍:https: //github.com/mustangotang/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.
纯PHP实现,安装时无需对服务器做任何修改。如果速度是一个可怕的问题,它可能不是理想的解决方案,但如果您使用 YAML 进行配置或使用量相对较低,这是一个很棒的解决方案。
Given a YAML document, Spyc will return an array that you can use however you see fit.
给定一个 YAML 文档,Spyc 将返回一个数组,您可以根据需要使用该数组。
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.
给定一个数组,Spyc 将返回一个字符串,其中包含从您的数据构建的 YAML 文档。
$yaml_str = Spyc::YAMLDump($myarray);
回答by Dan Powley
The symfony frameworkmakes very heavy use of YAML, this blog post by Grégtheitroade Hubertdemonstrates using their YAML library in a non-symfony project.
在symfony框架使得非常大量使用YAML,这的博客文章由格雷休伯特使用他们的YAML库在非symfony项目演示。
回答by Shane
Symfony2 has a YAML component which supports mostof the YAML 1.2 spec
Symfony2 有一个支持大部分YAML 1.2 规范的 YAML 组件
回答by bd808
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,您可能会发现纯 PHP 库(如 spyc 或 Symfony YAML)不够快。C YAML 解析器至少有两个 PHP 绑定:
回答by takeshin
Try sfYaml, it is the best I know.
尝试sfYaml,这是我所知道的最好的。
Symfony and Doctrine ORM are using this one.
Symfony 和 Doctrine ORM 正在使用这个。
To get it, you may Download Doctrine 1.2and extract sfYamlfrom vendordirectory.
要获得它,您可以下载 Doctrine 1.2并sfYaml从vendor目录中提取。
Let us know if it suits your needs.
让我们知道它是否适合您的需求。
回答by Paul Tarjan
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.
如果您需要快速测试您的 YAML,我构建了:http: //yaml-online-parser.appspot.com/。它帮助我编写 YAML,尤其是在学习的时候。
回答by preinheimer
I'd suggest the process followed in this article http://devzone.zend.com/article/2585-using-yaml-with-php-and-pecl
我建议在这篇文章中遵循的过程 http://devzone.zend.com/article/2585-using-yaml-with-php-and-pecl

