欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

例如配置文件

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

函數(shù)定義:

配置文件數(shù)據(jù)值獲?。篺unction 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)沒有第三個參數(shù)時,按照字符串讀取提取''中或""中的內(nèi)容
//如果有第三個參數(shù)時為int時按照數(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)沒有第四個參數(shù)時,按照字符串讀取提取''中或""中的內(nèi)容
//如果有第四個參數(shù)時為int時按照數(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)沒有第三個參數(shù)時,按照字符串讀取提取''中或""中的內(nèi)容
 * 如果有第三個參數(shù)時為int時按照數(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論