将php中的配置文件解析为变量 [英] Parsing a config file in php to variables

查看:106
本文介绍了将php中的配置文件解析为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-背景-

所以我正在为本地开发机器及其安装Wordpress的安装程序脚本工作,我想做的就是拥有它,以便用户从下拉框中选择一个特定的插件,然后从那里选择他们要安装的版本.

So I am working on a installer script for local dev machines and its for installing word press, What I am trying to do is have it so that the user would select a specific plug in from a drop down box and then from there select which version they want to install.

-问题-

所以我需要能够解析.cfg文件并将所有值动态存储到变量中,这样,如果我们添加更多插件,它就不会损坏.我该怎么做呢?

So I need to be able to parse the .cfg file and store all the values into variables dynamically so that way if we add more plugins it won't break. How do I go about doing this?

推荐答案

方法的一对,取决于您希望如何存储配置数据.最快的方法是将数据存储为.ini文件,然后使用PHP内置的 parse_ini_file 进行解析.您还可以将数据存储为其他格式,例如XML和YAML,但我不建议您使用XML,因为您可能不会在完全不同的系统之间传输数据,而且XML往往很难用所有无关的标签读取( vs yaml或ini).

Couple of ways, depending on how you wish to store the config data. The fastest way is to store the data as an .ini file and parse it using PHP's built in parse_ini_file. You could also store the data in other formats such as XML and YAML, but I wouldn't recommend XML as you probably won't be transporting the data betweeen disparate systems, also XML tends to be harder to read with all the extraneous tags (vs yaml or ini).

我个人更喜欢yaml,并使用 Symfony的Yaml组件解析器来解析yaml文件放入数组. Symfony的Yaml组件可以在Symfony框架之外使用.您也可以查看 Zend的Yaml解析器.

I personally prefer yaml and use Symfony's Yaml Component parser which will parse the yaml file into an array. Symfony's Yaml Component can be used outside of the Symfony framework. You could also look into Zend's Yaml parser as well.

在选择了要使用的格式和解析器之后,就像将配置文件存储在Web服务器可以访问的某个位置,需要它并通过解析器API传递一样容易.解析之后,您应该可以通过数组访问值.

After you pick the format and parser you wish to use, it's as easy as storing the config file somewhere that is accessible by your webserver, requiring it, and passing it through the parser API. After it is parsed you should have access to the values via an array.

-更新-

<?php

$settings = parse_ini_file('test.ini');

var_dump($settings);

结果:

array(41) {
["plugins"]=>
string(0) ""
["breadcrumb-navxt"]=>
string(5) "4.0.1"
["bp-moderation"]=>
string(5) "0.1.4"
["buddypress-activity-stream-hashtags"]=>
string(5) "0.4.0"
["buddypress-group-documents"]=>
string(5) "0.3.5"
["buddypress-group-email-subscription"]=>
string(5) "2.9.1"
["buddypress-links"]=>
string(3) "0.5"
["buddypress"]=>
string(6) "1.2.10"
["calendar"]=>
string(5) "1.3.1"
["collapsing-pages"]=>
string(5) "0.6.1"

这似乎按预期工作,因此,如果我想要calendar插件的版本号,我会这样做:

This appears to work as expected, so if I want the version number of the calendar plug-in I would just do:

var_dump($settings['calendar']);

要存储在动态变量中:

$settings = parse_ini_file('test.ini');

foreach ($settings as $key => $setting) {
    // Notice the double $$, this tells php to create a variable with the same name as key
    $$key = $setting; 
}

var_dump($calendar);

这篇关于将php中的配置文件解析为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆