php中配置文件操作 如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)沒有第三個(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)文章
PHP遞歸遍歷多維數(shù)組實(shí)現(xiàn)無限分類的方法
這篇文章主要介紹了PHP遞歸遍歷多維數(shù)組實(shí)現(xiàn)無限分類的方法,涉及PHP遞歸操作遍歷數(shù)組的相關(guān)技巧,在聯(lián)動(dòng)菜單及父子欄目設(shè)計(jì)等方面非常具有實(shí)用價(jià)值,需要的朋友可以參考下2016-05-05
PHP實(shí)現(xiàn)設(shè)計(jì)模式中的抽象工廠模式詳解
這篇文章主要介紹了PHP實(shí)現(xiàn)設(shè)計(jì)模式中的抽象工廠模式詳解,抽象工廠模式(Abstact Factory)是一種常見的軟件設(shè)計(jì)模式,需要的朋友可以參考下2014-10-10
php基于ob_start(ob_gzhandler)實(shí)現(xiàn)網(wǎng)頁壓縮功能的方法
這篇文章主要介紹了php基于ob_start('ob_gzhandler')實(shí)現(xiàn)網(wǎng)頁壓縮功能的方法,涉及php中ob_gzip、ob_start等函數(shù)操作緩沖區(qū)及內(nèi)容壓縮相關(guān)技巧,需要的朋友可以參考下2017-02-02
php中file_get_contents與curl性能比較分析
這篇文章主要介紹了php中file_get_contents與curl性能比較,以實(shí)例形式詳細(xì)分析了file_get_contents與curl的區(qū)別以及運(yùn)行效率的對(duì)比,需要的朋友可以參考下2014-11-11
phpy之PHP與Python互調(diào)庫實(shí)現(xiàn)AI編程
這篇文章主要為大家介紹了phpy之PHP與Python互調(diào)庫實(shí)現(xiàn)AI編程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
php實(shí)現(xiàn)發(fā)送微信模板消息的方法
這篇文章主要介紹了php實(shí)現(xiàn)發(fā)送微信模板消息的方法,實(shí)例分析了php操作curl及自定義模板消息的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
php刪除與復(fù)制文件夾及其文件夾下所有文件的實(shí)現(xiàn)代碼
最近寫代碼,需要個(gè)復(fù)制文件夾與及其文件夾下所有文件的功能,有時(shí)候需要?jiǎng)h除,就可以用這個(gè)函數(shù)了,通過xcopy與deldir的自定義函數(shù),確實(shí)方便多了,特分享下2013-01-01

