-
- /**
- desc: configuration file
- link: bbs.it-home.org
- date: 2013/2/24
- */
- $name="admin";//kkkk
- $bb='234';
- $db=4561321;
- $kkk="admin" ;
- ?>
Copy code
Function definition:
Get configuration file data value: function getconfig($file, $ini, $type="string")
Configuration file data item update: function updateconfig($file, $ini, $value,$type="string")
Calling method:
-
-
getconfig("./2.php", "bb");// - updateconfig("./2.php", "kkk", "admin ");
//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
- //By default, when there is no fourth parameter, read and extract the content in '' or "" according to the string
- //If there is a fourth parameter and it 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 following is the improved version
-
-
- //Perfect and improved version
- /**
- * link: bbs.it-home.org
- * date: 2013/2/24
- * Configuration file operation (query and modification)
- * By default, when there is no third parameter, read and extract '' according to the string Or the content in ""
- * If there is a third parameter that is an int, it will be processed as a numerical 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);
- }
- ?>
-
Copy code
Articles you may be interested in:
An example of php DES encryption and decryption code
php uses 3des encrypted code (compatible with .net)
|