开发时,经常会对一些临时数据做存储,又免不了创建临时数据表,而且这些数据可能也会随时发生变化,又少不了对数据库的读写操作,既麻烦又费时,那么这时候该如何妥善储存这些临时数据呢?将这些数据以配置形式存储到.ini文件再好不过了。特此用100行代码写了一个PHP对.ini文件操作的类,方便以后使用。
代码:
<?php /** * PHP操作ini文件类 * @author Wigiesen - 心语难诉 * @version v1.0 * @link https://xinyu19.com * 注:ini文件由节、键、值组成,为了方便 * 类中的[节]我们叫做[分类],[键=>值]称为[子项] */ class iniFile { public $iniFilePath; public $iniFileHandle; function __construct($iniFilePath) { $this->iniFilePath = $iniFilePath; # 读入 .ini 文件到句柄中 if (file_exists($this->iniFilePath)) { $this->iniFileHandle = parse_ini_file($this->iniFilePath, true); if (empty($this->iniFileHandle)) die($this->iniFilePath . ' file is null'); }else{ die($this->iniFilePath . ' file cannot be opened'); } } //增加分类 public function addCategory($category_name,array $item = []){ if (!isset($this->iniFileHandle[$category_name])) { $this->iniFileHandle[$category_name] = []; }else{ if (!empty($item)) { foreach ($item as $key => $value) { $this->iniFileHandle[$category_name][$key] = $value; } } } $this->save(); } //增加子项[可在添加分类的同时添加子项] public function addItem($category_name, array $item){ foreach ($item as $key => $value) { $this->iniFileHandle[$category_name][$key] = $value; } $this->save(); } //获取所有 public function getAll(){ return $this->iniFileHandle; } //获取单个分类 public function getCategory($category_name){ return $this->iniFileHandle[$category_name]; } //获取子项值 public function getItem($category_name, $item_name){ # 如果是获取多个子项,则循环读取放入新变量 if (is_array($item_name)) { $arr = array(); foreach ($item_name as $value) { $arr[$value] = $this->iniFileHandle[$category_name][$value]; } return $arr; }else{ return $this->iniFileHandle[$category_name][$item_name]; } } //更改ini public function updItem($category_name, array $item){ foreach ($item as $key => $value) { $this->iniFileHandle[$category_name][$key] = $value; } $this->save(); } //删除分类 public function delCategory($category_name){ unset($this->iniFileHandle[$category_name]); $this->save(); } //删除子项 public function delItem($category_name, $item_name) { unset($this->iniFileHandle[$category_name][$item_name]); $this->save(); } //保存.ini文件 public function save(){ $string = ''; # 循环句柄,拼接成ini格式的字符串 foreach ($this->iniFileHandle as $key => $value) { $string .= '['.$key.']'."rn"; foreach ($value as $k => $v) { $string .= "$k = $vrn"; } } $iniFileHandle = fopen($this->iniFilePath, 'w+'); $isfwrite = fwrite($iniFileHandle, $string); if ($isfwrite) { fclose($iniFileHandle); return true; }else{ fclose($iniFileHandle); return false; } } } ?>
使用方式:
# 实例化ini文件操作类,并载入 .ini文件 $iniFile = new iniFile('./config.ini'); # 添加一个分类 $iniFile->addCategory('config'); # 添加一个分类并直接添加子项 $iniFile->addCategory('config', ['test1' => 123, 'test2' => 456]); # 添加一个子项(如果子项存在,则覆盖;) $iniFile->addItem('config',['test1' => 123]); # 删除一个分类 $iniFile->delCategory('config'); # 删除一个子项 $iniFile->delItem('config', 'test1'); # 修改一个分类下子项的值(也可以修改多个) $iniFile->updItem('config', ['test1' => 789]); # 获取一个分类下所有数据 print_r($iniFile->getCategory('config')); # 获取一个分类下某个子项的值 print_r($iniFile->getItem('config','test1')); # 获取一个分类下多个子项的值 print_r($iniFile->getItem('config',['test1', 'test2'])); # 获取ini文件中所有分类及子项 print_r($iniFile->getAll()); # 或者 print_r($iniFile->iniFileHandle);
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
喜欢就支持以下吧