前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >100行代码实现PHP对.ini文件的CURD操作

100行代码实现PHP对.ini文件的CURD操作

作者头像
Inkedus
发布2020-04-16 15:13:00
8350
发布2020-04-16 15:13:00
举报
文章被收录于专栏:InkedusInkedus

开发时,经常会对一些临时数据做存储,又免不了创建临时数据表,而且这些数据可能也会随时发生变化,又少不了对数据库的读写操作,既麻烦又费时,那么这时候该如何妥善储存这些临时数据呢?将这些数据以配置形式存储到.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.']'."\r\n";
            foreach ($value as $k => $v) {
                $string .= "$k = $v\r\n";
            }
        }
        $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);
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019 年 08 月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档