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

 更新时间:2021年05月12日 12:23:57   投稿:mdxy-dxy  
对形如config.php文件的读取,修改等操作的代码,需要的朋友可以参考下

例如配置文件

<?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中配置文件操作 如config.php文件的读取修改等操作的文章就介绍到这了,更多相关配置文件操作内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • php-perl哈希算法实现(times33哈希算法)

    php-perl哈希算法实现(times33哈希算法)

    php-perl哈希实现算法–DJBX33A(Daniel J. Bernstein, Times 33 with Addition)APR哈希默认算法
    2013-12-12
  • php中删除字符串中最先出现某个字符的实现代码

    php中删除字符串中最先出现某个字符的实现代码

    删除字符串中最先出现某个字,就是通过explode的灵活用法,需要的朋友可以参考下
    2013-02-02
  • PHP合并discuz用户脚本的方法

    PHP合并discuz用户脚本的方法

    这篇文章主要介绍了PHP合并discuz用户脚本的方法,分析了合并discuz用户数据库的方法与相关实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • gearman中任务的优先级和返回状态实例分析

    gearman中任务的优先级和返回状态实例分析

    这篇文章主要介绍了gearman中任务的优先级和返回状态,结合实例形式分析了gearman任务的优先级以及获取返回状态相关操作技巧,需要的朋友可以参考下
    2020-02-02
  • php readfile下载大文件失败的解决方法

    php readfile下载大文件失败的解决方法

    这篇文章主要介绍了php readfile下载大文件失败的解决方法,涉及php针对大文件的分割及逐块下载操作实现技巧,需要的朋友可以参考下
    2017-05-05
  • php格式输出文件var_export函数实例

    php格式输出文件var_export函数实例

    这篇文章主要介绍了php格式输出文件var_export函数,以实例形式讲述了格式输出函数var_export的特性与具体用法,具有一定的参考借鉴价值,需要的朋友可以参考下
    2014-11-11
  • 详解PHP队列的实现

    详解PHP队列的实现

    这篇文章主要介绍了PHP队列的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • php 抽象类的简单应用

    php 抽象类的简单应用

    我想博客中的 文章列表和单个的文章阅读 统一起来,我觉得除了sql查询语句结构不同,HTML代码不同,其它也就一样。不过话说回来,这两个都是主要功能,所以这的确有点不适合,不过昨晚读了一点设计模式,好歹得写点啥好。
    2011-09-09
  • 功能强大的php分页函数

    功能强大的php分页函数

    这篇文章主要为大家分享了功能强大的php分页函数,为php初学者分析一下php分页实现方法,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • 关于PHP二进制流 逐bit的低位在前算法(详解)

    关于PHP二进制流 逐bit的低位在前算法(详解)

    本篇文章是对PHP二进制流逐bit的低位在前算法进行了详细的分析介绍,需要的朋友参考下
    2013-06-06

最新评论