Copy Code code as follows:
<?php
$name = "admin";//kkkk
$BB = ' 234 ';
$db = 4561321;
$KKK = "admin";
?>
function definition:
Profile data value fetch: function GetConfig ($file, $ini, $type = "string")
Profile Data Item Update: function Updateconfig ($file, $ini, $value, $type = "string")
Call Mode:
Copy Code code as follows:
GetConfig ("./2.php", "BB");
Updateconfig ("./2.php", "KKK", "admin");
Copy Code code as follows:
<?php
The profile data value is obtained.
The contents of the extract ' or ' in ' are read by string when there is no third parameter by default
If the third argument is int, it is treated as a numeric 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];
}
}
Profile Data Item Update
When the default does not have the fourth parameter, the content in the extract ' or ' is read by string
If the fourth argument is int, it is treated as a numeric int.
function Updateconfig ($file, $ini, $value, $type = "string")
{
$str = file_get_contents ($file);
$str 2 = "";
if ($type = = "int")
{
$str 2 = preg_replace ("/". $ini. "= (. *);/", $ini. " =" . $value. ";", $str);
}
Else
{
$str 2 = preg_replace ("/". $ini. "= (. *);/", $ini. " =\"" . $value. "\"; ", $str);
}
File_put_contents ($file, $str 2);
}
Echo GetConfig ("./2.php", "BB", "string");
GetConfig ("./2.php", "BB");
Updateconfig ("./2.php", "KKK", "admin");
echo "<br/>". GetConfig ("./2.php", "Name", "string");
?>
Copy Code code as follows:
Improve the improved version
/**
* Profile operation (queried and modified)
* The content in the extract ' or ' from the string is read by default when there is no third parameter
* If the third parameter is int, it is treated as a numeric int.
* Invoke 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);
$str 2 = "";
if ($type = = "int") {
$str 2 = preg_replace ("/". Preg_quote ($ini). = (. *);/", $ini." = ". $value."; ", $str);
}
else{
$str 2 = preg_replace ("/". Preg_quote ($ini). = (. *);/", $ini." =\ "". $value. " \ ";", $str);
}
File_put_contents ($file, $str 2);
}