色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

php中配置文件操作 如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技術php中配置文件操作 如config.php文件的讀取修改等操作,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 通州区| 潞城市| 清新县| 梅州市| 滨海县| 赫章县| 彩票| 清苑县| 宜宾市| 永仁县| 宁波市| 中西区| 仙游县| 子洲县| 静乐县| 洱源县| 辽宁省| 富裕县| 光泽县| 安丘市| 虞城县| 平江县| 大关县| 盐源县| 双峰县| 平陆县| 星座| 广汉市| 沁水县| 新乡市| 手游| 济南市| 浪卡子县| 永康市| 游戏| 邯郸县| 宝山区| 余姚市| 涡阳县| 襄樊市| 峡江县|