龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php ini文件编辑

时间:2014-11-29 15:02来源:网络整理 作者:网络 点击:
分享到:
ini文件编辑 parse_ini_file方法只能对ini文件进行读取操作,无法满足编辑的需求。今天刚好需要这个东西,也就写了一个简单的类文件,实现对ini文件的编辑操作。^_^[代码片段(196行)]

parse_ini_file 方法只能对ini文件进行读取操作,无法满足编辑的需求。今天刚好需要这个东西,也就写了一个简单的类文件,实现对ini文件的编辑操作。^_^

<?php

define('CONFIG_PARSER_LF', "\\n"); //如果为windows,请设置为\\r\\n

class ConfigParser
{
    private static $_instance = null;
    private $_config;
    private $_filePath;

    final private function __construct() {}

    final private function __clone() {}

    public static function instance($filePath)
    {
        if (empty(self::$_instance)) {
            self::$_instance = new ConfigParser();
        }

        self::$_instance->_filePath = $filePath;
        self::$_instance->_config = @parse_ini_file($filePath, true);
        if (empty(self::$_instance->_config)) {
            self::$_instance->_config = array();
        }

        return self::$_instance;
    }

    public function get()
    {
        //获取所有
        if (func_num_args() == 0) {
            return $this->_config;
        }

        //获取section
        if (func_num_args() == 1) {
            if (!isset($this->_config[func_get_arg(0)])) {
                return null;
            }
            return $this->_config[func_get_arg(0)];
        }

        //获取option
        if (func_num_args() == 2) {
            if (!isset($this->_config[func_get_arg(0)])) {
                return null;
            }

            $selection = $this->_config[func_get_arg(0)];
            if (!is_array($selection) || !isset($selection[func_get_arg(1)])) {
                return null;
            }

            return $selection[func_get_arg(1)];
        }
    }

    public function addSection($section)
    {
        if (isset($this->_config[$section])) {
            return false;
        }

        $this->_config[$section] = array();
        return true;
    }

    public function set()
    {
        if (in_array(func_num_args(), array(2, 3))) {
            return false;
        }

        //设置section
        if (func_num_args() == 2) {
            $this->_config[func_get_arg(0)] = func_get_arg(1);
            return true;
        }

        //设置option
        if (func_num_args() == 3) {
            if (!isset($this->_config[func_get_arg(0)])) {
                $this->_config[func_get_arg(0)] = array();
            }

            if (!is_array($this->_config[func_get_arg(0)])) {
                return false;
            }

            $this->_config[func_get_arg(0)][func_get_arg(1)] = func_get_arg(2);
            return true;
        }
    }

    public function del()
    {
        //删除所有
        if (func_num_args() == 0) {
            $this->_config = array();
            return true;
        }

        //删除section
        if (func_num_args() == 1) {
            if (isset($this->_config[func_get_arg(0)])) {
                unset($this->_config[func_get_arg(0)]);
                return true;
            }
        }

        //删除option
        if (func_num_args() == 2) {
            if (!isset($this->_config[func_get_arg(0)])
                || !is_array($this->_config[func_get_arg(0)])
            ) {
                return false;
            }

            if (!isset($this->_config[func_get_arg(0)][func_get_arg(1)])) {
                return false;
            }

            unset($this->_config[func_get_arg(0)][func_get_arg(1)]);
            return true;
        }

        return false;
    }

    public function write()
    {
        if (empty($this->_fileName)) {
            $this->_config = array();
            return false;
        }

        $contents = array();
        $contentsNotInSection = array();
        if (!empty($this->_config)) {
            foreach ($this->_config as $section => $sectionItem) {
                 if (is_array($sectionItem)) {
                     array_push($contents, "[{$section}]");
                     if (!empty($sectionItem)) {
                         foreach ($sectionItem as $key => $value) {
                             array_push($contents, "{$key} = {$value}");
                         }
                     }
                     continue;
                 }
                 array_push(
                    $contentsNotInSection, "{$section} = {$sectionItem}"
                 );
            }
            $contents = array_unique($contents);
            $contentsNotInSection = array_unique($contentsNotInSection);
            $contents = array_merge($contentsNotInSection, $contents);
        }

        return file_put_contents(
            $this->_filePath, implode(CONFIG_PARSER_LF, $contents)
        );
    }
}

//使用说明
//config.ini
//gmail = hzlhu.dargon@gmail.com
//[section]
//name = value

$config = ConfigParser::instance('config.ini');
//读取
$config->get();
$config->get('gmail');
$config->get('section', 'name');

//添加section
$config->addSection('test');

//设置
$config->set('gmail', 'hzlhu.dargon@gmail.com');
$config->set('section', array('name' => 'value'));
$config->set('section', 'name', 'value');

//删除
$config->del();
$config->del('gmail');
$config->del('section');
$config->del('section', 'name');

//将修改后的值保存到文件中
$config->write();
//该片段来自于http://outofmemory.cn
精彩图集

赞助商链接