Copy code The code is as follows:
$name="admin";//kkkk
$ bb='234';
$db=4561321;
$kkk="admin";
?>
Function definition:
Configuration file data value acquisition :function getconfig($file, $ini, $type="string")
Configuration file data item update: function updateconfig($file, $ini, $value,$type="string")
Calling method :
Copy code The code is as follows:
getconfig("./2.php", "bb");//
updateconfig("./2.php", "kkk", "admin");
Copy code The code is as follows :
//Get configuration file data value.
//By default, when there is no third parameter, read and extract the content in '' or "" according to the string
//If there is a third parameter, it is int, and it is processed as a digital 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];
}
}
//Configuration file data item update
//There is no fourth parameter by default When, read and extract the content in '' or "" according to the string
//If there is a fourth parameter that is int, it will be processed as a digital 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 "
".getconfig("./2.php", "name","string");
?>
Copy code The code is as follows:
//Perfect and improved version
/**
* Configuration file operation (query and modification)
* By default, when there is no third parameter, read and extract the content in '' or "" according to the string
* If there is a third parameter When it is an int, it is treated as a numeric int.
*Call 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);
}
http://www.bkjia.com/PHPjc/325679.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325679.htmlTechArticleCopy the code as follows: ?php $name="admin";//kkkk $bb='234'; $db=4561321; $kkk="admin"; ? Function definition: Configuration file data value acquisition: function getconfig($file, $ini, $type="st...