php INI配置文件的解析實(shí)現(xiàn)分析
更新時(shí)間:2011年01月04日 23:10:40 作者:
我不知道怎么說(shuō)才好,因?yàn)槲以谧xINI文件的時(shí)候,往往都是用現(xiàn)成的函數(shù):parse_ini_file或者是parse_ini_string,但怎么寫(xiě)入,就是另外的方法了(自己實(shí)現(xiàn)。。。。)
所以看到這篇文章的時(shí)候,我也才剛剛知道,原來(lái),還有一個(gè)dba的函數(shù)可以用,嗯,仔細(xì)看了一下dba這個(gè)函數(shù)的installtion,發(fā)現(xiàn)支持inifile也是從PHP5才開(kāi)始實(shí)現(xiàn)的。好吧,相應(yīng)的dba相關(guān)的可以看看這里:http://www.php.net/manual/en/dba.installation.php,詳細(xì)的還是看這里吧:http://www.php.net/manual/en/book.dba.php
OK,上原文,它來(lái)自于:http://www.cardii.net/php-spl-parse-ini-file/。
曾經(jīng)介紹過(guò)SPL的各類(lèi)型接口和迭代器。今天,在瀏覽PHP源碼目錄時(shí),發(fā)現(xiàn)有個(gè)解析INI文件的例子,覺(jué)得不錯(cuò),于是整理了一個(gè)實(shí)例,拿來(lái)分享下。
在PHP應(yīng)用程序中,配置文件不可或缺,特別是商城,CMS之類(lèi)的產(chǎn)品,不同的客戶(hù)需求不同,當(dāng)然,不會(huì)每個(gè)客戶(hù)開(kāi)發(fā)一套程序,好辦法的是每個(gè)客戶(hù) 有一套不同的配置文件。適合做配置文件的我曾經(jīng)也說(shuō)過(guò),主要有四類(lèi):PHP數(shù)組(幾乎其他的配置方法最終都是解析成為PHP數(shù)組),XML,YAML和 INI。今天只講INI文件。ZendFramework使用此配置。
下看個(gè)DbaReader類(lèi)。文件名為 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接口,當(dāng)然要實(shí)現(xiàn)里面的5個(gè)迭代方法。迭代方法對(duì)handlerhandlerINI文件的解析,用到了dba擴(kuò)展。
說(shuō)點(diǎn)題外話,什么是Dba?為什么使用Dba?
Dba是一款數(shù)據(jù)庫(kù),確切點(diǎn)說(shuō),是一款索引化的文件存儲(chǔ)系統(tǒng)。適合相對(duì)比較靜態(tài)的索引化的數(shù)據(jù)存儲(chǔ)。所有版本的Linux都會(huì)帶此數(shù)據(jù)庫(kù)。
既然使用文件來(lái)存儲(chǔ)數(shù)據(jù),為什么還有使用Dba呢?原因有二:
1數(shù)據(jù)記錄的存儲(chǔ)長(zhǎng)度可以不是固定的;
2使用索引存儲(chǔ)和檢索數(shù)據(jù)。
DbaReader提供一個(gè)訪問(wèn)INI文件數(shù)據(jù)的迭代方法,如果需要存儲(chǔ)刪除數(shù)據(jù)呢?所以DbaArray在繼承DbaReader的基礎(chǔ)上,實(shí)現(xiàn)了此功能。
<?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);
}
}
?>
使用范例
構(gòu)建文件text.ini,內(nèi)容如下:
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--
看完上面這一段,是不是有什么想法?原來(lái)ini的操作也是這么的方便?不過(guò),如果是純讀取的話,我還是比較推薦于parse_ini_file之類(lèi)的(突然間忘了,如果編碼不一樣怎么辦?ansi/utf-8,這真是一個(gè)永恒的痛。)
OK,上原文,它來(lái)自于:http://www.cardii.net/php-spl-parse-ini-file/。
曾經(jīng)介紹過(guò)SPL的各類(lèi)型接口和迭代器。今天,在瀏覽PHP源碼目錄時(shí),發(fā)現(xiàn)有個(gè)解析INI文件的例子,覺(jué)得不錯(cuò),于是整理了一個(gè)實(shí)例,拿來(lái)分享下。
在PHP應(yīng)用程序中,配置文件不可或缺,特別是商城,CMS之類(lèi)的產(chǎn)品,不同的客戶(hù)需求不同,當(dāng)然,不會(huì)每個(gè)客戶(hù)開(kāi)發(fā)一套程序,好辦法的是每個(gè)客戶(hù) 有一套不同的配置文件。適合做配置文件的我曾經(jīng)也說(shuō)過(guò),主要有四類(lèi):PHP數(shù)組(幾乎其他的配置方法最終都是解析成為PHP數(shù)組),XML,YAML和 INI。今天只講INI文件。ZendFramework使用此配置。
下看個(gè)DbaReader類(lèi)。文件名為 DbaReader.php:
復(fù)制代碼 代碼如下:
<?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接口,當(dāng)然要實(shí)現(xiàn)里面的5個(gè)迭代方法。迭代方法對(duì)handlerhandlerINI文件的解析,用到了dba擴(kuò)展。
說(shuō)點(diǎn)題外話,什么是Dba?為什么使用Dba?
Dba是一款數(shù)據(jù)庫(kù),確切點(diǎn)說(shuō),是一款索引化的文件存儲(chǔ)系統(tǒng)。適合相對(duì)比較靜態(tài)的索引化的數(shù)據(jù)存儲(chǔ)。所有版本的Linux都會(huì)帶此數(shù)據(jù)庫(kù)。
既然使用文件來(lái)存儲(chǔ)數(shù)據(jù),為什么還有使用Dba呢?原因有二:
1數(shù)據(jù)記錄的存儲(chǔ)長(zhǎng)度可以不是固定的;
2使用索引存儲(chǔ)和檢索數(shù)據(jù)。
DbaReader提供一個(gè)訪問(wèn)INI文件數(shù)據(jù)的迭代方法,如果需要存儲(chǔ)刪除數(shù)據(jù)呢?所以DbaArray在繼承DbaReader的基礎(chǔ)上,實(shí)現(xiàn)了此功能。
復(fù)制代碼 代碼如下:
<?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);
}
}
?>
使用范例
構(gòu)建文件text.ini,內(nèi)容如下:
復(fù)制代碼 代碼如下:
host = localhost
password = password
database = data
文件index.php.代碼如下:
復(fù)制代碼 代碼如下:
<?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--
看完上面這一段,是不是有什么想法?原來(lái)ini的操作也是這么的方便?不過(guò),如果是純讀取的話,我還是比較推薦于parse_ini_file之類(lèi)的(突然間忘了,如果編碼不一樣怎么辦?ansi/utf-8,這真是一個(gè)永恒的痛。)
相關(guān)文章
thinkphp 手機(jī)號(hào)和用戶(hù)名同時(shí)登錄
本文主要介紹了實(shí)現(xiàn)手機(jī)號(hào)和用戶(hù)名同時(shí)登錄的方法。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01PHP大批量數(shù)據(jù)操作時(shí)臨時(shí)調(diào)整內(nèi)存與執(zhí)行時(shí)間的方法
PHP大批量數(shù)據(jù)操作時(shí)臨時(shí)調(diào)整內(nèi)存與執(zhí)行時(shí)間的方法 ,因?yàn)檫\(yùn)行的程序會(huì)很大很慢所以需要臨時(shí)設(shè)置下。不用修改php.ini參數(shù)了。2011-04-04libmysql.dll與php.ini是否真的要拷貝到c:\windows目錄下呢
很多安裝PHP的教程,都是教大家把php里的libmysql.dll拷貝到c:\windows目錄下(有的教程會(huì)說(shuō)還要把php.ini等文件拷到系統(tǒng)目錄的,其實(shí)一個(gè)文件都不用拷貝去的。)。2010-03-03PHP替換Word中變量并導(dǎo)出PDF圖片的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于PHP替換Word中變量并導(dǎo)出PDF圖片的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11