您的位置:首页 > 其它

INI配置文件的解析

2011-02-09 10:31 190 查看
我不知道怎么说才好,因为我在读INI文件的时候,往往都是用现成的函数:parse_ini_file或者是parse_ini_string,但怎么写入,就是另外的方法了(自己实现。。。。)

所以看到这篇文章的时候,我也才刚刚知道,原来,还有一个dba的函数可以用,嗯,仔细看了一下dba这个函数的installtion,发现支持inifile也是从PHP5才开始实现的。好吧,相应的dba相关的可以看看这里:http://www.php.net/manual/en/dba.installation.php,详细的还是看这里吧:http://www.php.net/manual/en/book.dba.php

OK,上原文,它来自于:http://www.cardii.net/php-spl-parse-ini-file/。

曾经介绍过SPL的各类型接口和迭代器。今天,在浏览PHP源码目录时,发现有个解析INI文件的例子,觉得不错,于是整理了一个实例,拿来分享下。

在PHP应用程序中,配置文件不可或缺,特别是商城,CMS之类的产品,不同的客户需求不同,当然,不会每个客户开发一套程序,好办法的是每个客户 有一套不同的配置文件。适合做配置文件的我曾经也说过,主要有四类:PHP数组(几乎其他的配置方法最终都是解析成为PHP数组),XML,YAML和 INI。今天只讲INI文件。ZendFramework使用此配置。

下看个DbaReader类。文件名为 DbaReader.php:

[code]<?php
class DbaArray extends DbaReader implements ArrayAccess
{

/**
* Open database $file with $handler in read only mode.
*
* @param file    Database file to open.
* @param handler Handler to use for database access.取值http://www.php.net/manual/en/dba.requirements.php
*/
function __construct($file, $handler)
{
$this->db = dba_popen($file, "c", $handler);
if (!$this->db) {
throw new exception("Databse could not be opened");
}
}

/**
* Close database.
*/
function __destruct()
{
parent::__destruct();
}

/**
* Read an entry.
*
* @param $name key to read from
* @return value associated with $name
*/
function offsetGet($name)
{
$data = dba_fetch($name, $this->db);
if($data) {
if (ini_get('magic_quotes_runtime')) {
$data = stripslashes($data);
}
//return unserialize($data);
return $data;
}
else
{
return NULL;
}
}

/**
* Set an entry.
*
* @param $name key to write to
* @param $value value to write
*/
function offsetSet($name, $value)
{
//dba_replace($name, serialize($value), $this->db);
dba_replace($name, $value, $this->db);
return $value;
}

/**
* @return whether key $name exists.
*/
function offsetExists($name)
{
return dba_exists($name, $this->db);
}

/**
* Delete a key/value pair.
*
* @param $name key to delete.
*/
function offsetUnset($name)
{
return dba_delete($name, $this->db);
}
}
?>


使用范例
构建文件text.ini,内容如下:


 ]
<?php
function loadClass($class)
{
require_once __DIR__.DIRECTORY_SEPARATOR.$class.'.php';
}
spl_autoload_register('loadClass',false);

$iniFile = __DIR__.DIRECTORY_SEPARATOR.'test.ini';

$ini = new DbaArray($iniFile,'iniFile');
echo $ini['database'];
var_dump($ini);
?>


--EOF--

看完上面这一段,是不是有什么想法?原来ini的操作也是这么的方便?不过,如果是纯读取的话,我还是比较推荐于parse_ini_file之类的(突然间忘了,如果编码不一样怎么办?ansi/utf-8,这真是一个永恒的痛。)

来源:http://www.neatstudio.com/show-1570-1.shtml
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: