php INI配置文件的解析实现分析
2015-01-24信息快讯网
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:
<?php
class DbaReader implements Iterator
{
protected $db = NULL;
private $key = false;
private $val = false;
/**
* Open database $file with $handler in read only mode.
*
* @param file Database file to open.
* @param handler Handler to use for database access.
*/
function __construct($file, $handler) {
if (!$this->db = dba_open($file, 'r', $handler)) {
throw new exception('Could not open file ' . $file);
}
}
/**
* Close database.
*/
function __destruct() {
dba_close($this->db);
}
/**
* Rewind to first element.
*/
function rewind() {
$this->key = dba_firstkey($this->db);
$this->fetch_data();
}
/**
* Move to next element.
*
* @return void
*/
function next() {
$this->key = dba_nextkey($this->db);
$this->fetch_data();
}
/**
* Fetches the current data if $key is valid
*/
private function fetch_data() {
if ($this->key !== false) {
$this->val = dba_fetch($this->key, $this->db);
}
}
/**
* @return Current data.
*/
function current() {
return $this->val;
}
/**
* @return Whether more elements are available.
*/
function valid() {
if ($this->db && $this->key !== false) {
return true;
} else {
return false;
}
}
/**
* @return Current key.
*/
function key() {
return $this->key;
}
}
?>
DbaReader使用Iterator接口,当然要实现里面的5个迭代方法。迭代方法对handlerhandlerINI文件的解析,用到了dba扩展。
说点题外话,什么是Dba?为什么使用Dba?
Dba是一款数据库,确切点说,是一款索引化的文件存储系统。适合相对比较静态的索引化的数据存储。所有版本的Linux都会带此数据库。
既然使用文件来存储数据,为什么还有使用Dba呢?原因有二:
1数据记录的存储长度可以不是固定的;
2使用索引存储和检索数据。
DbaReader提供一个访问INI文件数据的迭代方法,如果需要存储删除数据呢?所以DbaArray在继承DbaReader的基础上,实现了此功能。
<?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,内容如下:
host = localhost password = password database = data
文件index.php.代码如下:
<?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,这真是一个永恒的痛。)
无法在发生错误时创建会话,请检查 PHP 或网站服务器日志,并正确配置 PHP 安装(win+linux)
php截取后台登陆密码的代码
php在线代理转向代码
超小PHP小马小结(方便查找后门的朋友)
apache mysql php 源码编译使用方法
PHP 基于文件头的文件类型验证类函数
PHPMYADMIN导入数据最大为2M的解决方法
php 文件上传实例代码
php递归创建和删除文件夹的代码小结
PHP文件注释标记及规范小结
用PHP读取超大文件的实例代码
PHP新手用的Insert和Update语句构造类
phpmyadmin3 安装配置图解教程
MySQL的FIND_IN_SET函数使用方法分享
php.ini中date.timezone设置分析
Warning: session_destroy() : Trying to destroy uninitialized sessionq错误
php中神奇的fastcgi_finish_request
适用于php-5.2 的 php.ini 中文版[金步国翻译]
php批量缩放图片的代码[ini参数控制]
Discuz Uchome ajaxpost小技巧
PHP strncasecmp字符串比较的小技巧
php simplexmlElement操作xml的命名空间实现代码
array_multisort实现PHP多维数组排序示例讲解
php关于array_multisort多维数组排序的使用说明
PHP 设置MySQL连接字符集的方法
从php核心代码分析require和include的区别
深入理解PHP之require/include顺序 推荐
linux下删除7天前日志的代码(php+shell)
PHP中simplexml_load_string函数使用说明
window+nginx+php环境配置 附配置搭配说明
PHP 图片文件上传实现代码
php目录操作函数之获取目录与文件的类型
上传文件先创建目录 再上传到目录里面去
批量修改RAR文件注释的php代码
在PHP中使用curl_init函数的说明
php运行出现Call to undefined function curl_init()的解决方法
《PHP编程最快明白》第五讲:php目录、文件操作
PHP最常用的ini函数分析 针对PHP.ini配置文件
Fatal error: Call to undefined function curl_init()解决方法
libmysql.dll与php.ini是否真的要拷贝到c:\windows目录下呢