国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

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

2024-05-04 23:14:34
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:


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


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

復(fù)制代碼 代碼如下:


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


復(fù)制代碼 代碼如下:


<?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ù)項更新
//默認(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");

?>


復(fù)制代碼 代碼如下:


//完善改進(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);
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 尼勒克县| 兴海县| 邻水| 东台市| 岳阳市| 边坝县| 于田县| 邳州市| 武邑县| 措美县| 阳高县| 沈丘县| 墨玉县| 泾源县| 潜山县| 迁西县| 新疆| 抚远县| 平泉县| 博湖县| 巴塘县| 商南县| 合川市| 阿鲁科尔沁旗| 鲜城| 丹东市| 睢宁县| 宁安市| 全南县| 鱼台县| 巧家县| 黄陵县| 白水县| 绍兴县| 青龙| 尉氏县| 米脂县| 格尔木市| 元阳县| 东丽区| 巨鹿县|