Configuration file operations in php such as reading and modifying config.php files
- 2020-05-17 05:02:30
- OfStack
<?php
$name="admin";//kkkk
$bb='234';
$db=4561321;
$kkk="admin";
?>
Function definition:
Configuration file data value acquisition: function getconfig($file, $ini, $type="string")
Profile data item update: function updateconfig($file, $ini, $value,$type="string")
Method of invocation:
getconfig("./2.php", "bb");//
updateconfig("./2.php", "kkk", "admin");
<?php
// Configuration file data values.
// There is no default 3 When the number of parameters, read and extract according to the string '' Or in the "" The contents of the
// If you have the first 3 Is when int Time by number int To deal with.
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];
}
}
// Profile data item update
// There is no default 4 When the number of parameters, read and extract according to the string '' Or in the "" The contents of the
// If you have the first 4 Is when int Time by number int To deal with.
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");
?>
// Improved version
/**
* Configuration file operation ( Queried and modified )
* There is no default 3 When the number of parameters, read and extract according to the string '' Or in the "" The contents of the
* If you have the first 3 Is when int Time by number int To deal with.
* 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);
}