最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2021年05月12日 12:23:57   投稿:mdxy-dxy  
對(duì)形如config.php文件的讀取,修改等操作的代碼,需要的朋友可以參考下

例如配置文件

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

函數(shù)定義:

配置文件數(shù)據(jù)值獲取:function getconfig($file, $ini, $type="string")
配置文件數(shù)據(jù)項(xiàng)更新:function updateconfig($file, $ini, $value,$type="string")
調(diào)用方式:

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

<?php
//配置文件數(shù)據(jù)值獲取。
//默認(rèn)沒有第三個(gè)參數(shù)時(shí),按照字符串讀取提取''中或""中的內(nèi)容
//如果有第三個(gè)參數(shù)時(shí)為int時(shí)按照數(shù)字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];
    }
}
//配置文件數(shù)據(jù)項(xiàng)更新
//默認(rèn)沒有第四個(gè)參數(shù)時(shí),按照字符串讀取提取''中或""中的內(nèi)容
//如果有第四個(gè)參數(shù)時(shí)為int時(shí)按照數(shù)字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");

?>

完善改進(jìn)版

//完善改進(jìn)版
/**
 * 配置文件操作(查詢了與修改)
 * 默認(rèn)沒有第三個(gè)參數(shù)時(shí),按照字符串讀取提取''中或""中的內(nèi)容
 * 如果有第三個(gè)參數(shù)時(shí)為int時(shí)按照數(shù)字int處理。
 *調(diào)用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);
}

到此這篇關(guān)于php中配置文件操作 如config.php文件的讀取修改等操作的文章就介紹到這了,更多相關(guān)配置文件操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

砚山县| 永川市| 泰和县| 图木舒克市| 襄垣县| 孟村| 凤山县| 浦北县| 盘山县| 通城县| 海盐县| 中宁县| 宜兴市| 通城县| 晋城| 高尔夫| 河曲县| 大同县| 郧西县| 江陵县| 响水县| 乐平市| 浦城县| 宜昌市| 盘山县| 军事| 北安市| 屯门区| 新闻| 林西县| 万全县| 阿拉善右旗| 唐河县| 蒙自县| 江都市| 镇原县| 涞源县| 加查县| 巨野县| 侯马市| 绥中县|