石灰土做击实检测网站怎么填,模板下载免费网站,手机推广平台有哪些,wordpress 删除修订版本我不知道怎么说才好#xff0c;因为我在读INI文件的时候#xff0c;往往都是用现成的函数#xff1a;parse_ini_file或者是parse_ini_string#xff0c;但怎么写入#xff0c;就是另外的方法了(自己实现。。。。)所以看到这篇文章的时候#xff0c;我也才刚刚知道#x…我不知道怎么说才好因为我在读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.phpOK上原文它来自于http://www.cardii.net/php-spl-parse-ini-file/。曾经介绍过SPL的各类型接口和迭代器。今天在浏览PHP源码目录时发现有个解析INI文件的例子觉得不错于是整理了一个实例拿来分享下。在PHP应用程序中配置文件不可或缺特别是商城CMS之类的产品不同的客户需求不同当然不会每个客户开发一套程序好办法的是每个客户 有一套不同的配置文件。适合做配置文件的我曾经也说过主要有四类PHP数组(几乎其他的配置方法最终都是解析成为PHP数组)XML,YAML和 INI。今天只讲INI文件。ZendFramework使用此配置。下看个DbaReader类。文件名为 DbaReader.phpclass 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为什么使用DbaDba是一款数据库确切点说是一款索引化的文件存储系统。适合相对比较静态的索引化的数据存储。所有版本的Linux都会带此数据库。既然使用文件来存储数据为什么还有使用Dba呢原因有二数据记录的存储长度可以不是固定的使用索引存储和检索数据。DbaReader提供一个访问INI文件数据的迭代方法如果需要存储删除数据呢所以DbaArray在继承DbaReader的基础上实现了此功能。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 localhostpassword passworddatabase data文件index.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这真是一个永恒的痛。)