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)沒有第三個參數(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)文章
php-perl哈希算法實(shí)現(xiàn)(times33哈希算法)
php-perl哈希實(shí)現(xiàn)算法–DJBX33A(Daniel J. Bernstein, Times 33 with Addition)APR哈希默認(rèn)算法2013-12-12
php中刪除字符串中最先出現(xiàn)某個字符的實(shí)現(xiàn)代碼
刪除字符串中最先出現(xiàn)某個字,就是通過explode的靈活用法,需要的朋友可以參考下2013-02-02
gearman中任務(wù)的優(yōu)先級和返回狀態(tài)實(shí)例分析
這篇文章主要介紹了gearman中任務(wù)的優(yōu)先級和返回狀態(tài),結(jié)合實(shí)例形式分析了gearman任務(wù)的優(yōu)先級以及獲取返回狀態(tài)相關(guān)操作技巧,需要的朋友可以參考下2020-02-02
php格式輸出文件var_export函數(shù)實(shí)例
這篇文章主要介紹了php格式輸出文件var_export函數(shù),以實(shí)例形式講述了格式輸出函數(shù)var_export的特性與具體用法,具有一定的參考借鑒價值,需要的朋友可以參考下2014-11-11
關(guān)于PHP二進(jìn)制流 逐bit的低位在前算法(詳解)
本篇文章是對PHP二進(jìn)制流逐bit的低位在前算法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06

