php中配置文件操作 如config.php文件的读取修改等操作

所属分类: 网络编程 / PHP编程 阅读数: 2009
收藏 0 赞 0 分享

例如配置文件

<?php 
$name="admin";//kkkk 
$bb='234'; 
$db=4561321; 
$kkk="admin"; 
?> 

函数定义:

配置文件数据值获取:function getconfig($file, $ini, $type="string")
配置文件数据项更新:function updateconfig($file, $ini, $value,$type="string")
调用方式:

getconfig("./2.php", "bb");//
updateconfig("./2.php", "kkk", "admin");

<?php
//配置文件数据值获取。
//默认没有第三个参数时,按照字符串读取提取''中或""中的内容
//如果有第三个参数时为int时按照数字int处理。
function getconfig($file, $ini, $type = "string") {
    if ($type == "int") {
        $str = file_get_contents($file);
        $config = preg_match("/" . $ini . "=(.*);/", $str, $res);
        Return $res[1];
    } else {
        $str = file_get_contents($file);
        $config = preg_match("/" . $ini . "=\"(.*)\";/", $str, $res);
        if ($res[1] == null) {
            $config = preg_match("/" . $ini . "='(.*)';/", $str, $res);
        }
        Return $res[1];
    }
}
//配置文件数据项更新
//默认没有第四个参数时,按照字符串读取提取''中或""中的内容
//如果有第四个参数时为int时按照数字int处理。
function updateconfig($file, $ini, $value, $type = "string") {
    $str = file_get_contents($file);
    $str2 = "";
    if ($type == "int") {
        $str2 = preg_replace("/" . $ini . "=(.*);/", $ini . "=" . $value . ";", $str);
    } else {
        $str2 = preg_replace("/" . $ini . "=(.*);/", $ini . "=\"" . $value . "\";", $str);
    }
    file_put_contents($file, $str2);
}
//echo getconfig("./2.php", "bb", "string");
getconfig("./2.php", "bb"); //
updateconfig("./2.php", "kkk", "admin");
//echo "<br/>".getconfig("./2.php", "name","string");

?>

完善改进版

//完善改进版
/**
 * 配置文件操作(查询了与修改)
 * 默认没有第三个参数时,按照字符串读取提取''中或""中的内容
 * 如果有第三个参数时为int时按照数字int处理。
 *调用demo
    $name="admin";//kkkk
    $bb='234';
    $bb=getconfig("./2.php", "bb", "string");
    updateconfig("./2.php", "name", "admin");
*/
function get_config($file, $ini, $type="string"){
    if(!file_exists($file)) return false;
    $str = file_get_contents($file);
    if ($type=="int"){
        $config = preg_match("/".preg_quote($ini)."=(.*);/", $str, $res);
        return $res[1];
    }
    else{
        $config = preg_match("/".preg_quote($ini)."=\"(.*)\";/", $str, $res);
        if($res[1]==null){ 
            $config = preg_match("/".preg_quote($ini)."='(.*)';/", $str, $res);
        }
        return $res[1];
    }
}

function update_config($file, $ini, $value,$type="string"){
    if(!file_exists($file)) return false;
    $str = file_get_contents($file);
    $str2="";
    if($type=="int"){  
        $str2 = preg_replace("/".preg_quote($ini)."=(.*);/", $ini."=".$value.";",$str);
    }
    else{
        $str2 = preg_replace("/".preg_quote($ini)."=(.*);/",$ini."=\"".$value."\";",$str);
    }
    file_put_contents($file, $str2);
}
更多精彩内容其他人还在看

php导出CSV抽象类实例

这篇文章主要介绍了php导出CSV抽象类及其用法示例,可实现循环导出功能,从而避免内存不足的问题,需要的朋友可以参考下
收藏 0 赞 0 分享

php实现的zip文件内容比较类

这篇文章主要介绍了php实现的zip文件内容比较类及其用法,可实现比较两个zip文件的内容,返回新增、删除、及相同的文件列表,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

php实现获取及设置用户访问页面语言类

这篇文章主要介绍了php实现获取及设置用户访问页面语言类,可实现获取/设置用户访问的页面语言,如果用户没有设置访问语言,则读取Accept-Language,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP+FFMPEG实现将视频自动转码成H264标准Mp4文件

最近做一个在线教学网的项目,需要实现上传任意格式视频自动为h264标准视频,使用html5播放。最终使用PHP+FFMPEG实现,在此将详细解决方案分享给大家!
收藏 0 赞 0 分享

PHP会话控制:Session与Cookie详解

这篇文章主要介绍了PHP会话控制:Session与Cookie详解,本文详细讲解了PHP中Session与Cookie的相关知识,涵盖面较广,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP实现手机号码中间四位用星号(*)隐藏的自定义函数分享

这篇文章主要介绍了PHP实现手机号码中间四位用星号(*)隐藏的自定义函数分享,这是一个比较常用的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP获取mysql数据表的字段名称和详细信息的方法

这篇文章主要介绍了PHP获取mysql数据表的字段名称和详细信息的方法,本文同时还给出了获取数据表结构、列出数据库数据表等方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP中的output_buffering详细介绍

这篇文章主要介绍了PHP中的output_buffering详细介绍,本文讲解了output buffering的一些高级用法,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP错误Warning: Cannot modify header information - headers already sent by解决方法

这篇文章主要介绍了PHP错误Warning: Cannot modify header information - headers already sent by解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP常用编译参数中文说明

这篇文章主要介绍了PHP常用编译参数中文说明,本文用详细的中文注解了PHP编译参数的作用,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多