配置文件操作.如config.php文件的读取修改等操作

对形如config.php文件的读取,修改等操作 
<?php
    $name="admin";//kkkk
    $bb='234';
    $db=4561321;
    $kkk="admin";
?>

函数定义: 
    配置文件数据值获取:function getconfig($file, $ini, $type="string") 
    配置文件数据项更新:function updateconfig($file, $ini, $value,$type="string") 

调用方式: 
    getconfig("./2.php", "bb");//
    updateconfig("./2.php", "kkk", "admin");
 
标签: <无>
 

代码片段(2)

[代码] [PHP]代码

01 <?php
02  
03 //配置文件数据值获取。
04 //默认没有第三个参数时,按照字符串读取提取''中或""中的内容
05 //如果有第三个参数时为int时按照数字int处理。
06 function getconfig($file, $ini, $type="string")
07 {
08     if ($type=="int")
09     {
10         $str = file_get_contents($file);
11         $config = preg_match("/" . $ini . "=(.*);/", $str, $res);
12         Return $res[1];
13     }
14     else
15     {
16         $str = file_get_contents($file);
17         $config = preg_match("/" . $ini . "=\"(.*)\";/", $str, $res);
18         if($res[1]==null)
19         {  
20             $config = preg_match("/" . $ini . "='(.*)';/", $str, $res);
21         }
22         Return $res[1];
23     }
24 }
25  
26 //配置文件数据项更新
27 //默认没有第四个参数时,按照字符串读取提取''中或""中的内容
28 //如果有第四个参数时为int时按照数字int处理。
29 function updateconfig($file, $ini, $value,$type="string")
30 {
31     $str = file_get_contents($file);
32     $str2="";
33     if($type=="int")
34     {  
35         $str2 = preg_replace("/" . $ini . "=(.*);/", $ini . "=" . $value . ";", $str);
36     }
37     else
38     {
39         $str2 = preg_replace("/" . $ini . "=(.*);/", $ini . "=\"" . $value . "\";",$str);
40     }
41     file_put_contents($file, $str2);
42 }
43  
44  
45 //echo getconfig("./2.php", "bb", "string");
46 getconfig("./2.php", "bb");//
47 updateconfig("./2.php", "kkk", "admin");
48 //echo "<br/>".getconfig("./2.php", "name","string");
49  
50 ?>

[代码] [PHP]代码

01 //完善改进版
02  
03  
04 /**
05  * 配置文件操作(查询了与修改)
06  * 默认没有第三个参数时,按照字符串读取提取''中或""中的内容
07  * 如果有第三个参数时为int时按照数字int处理。
08  *调用demo
09     $name="admin";//kkkk
10     $bb='234';
11      
12     $bb=getconfig("./2.php", "bb", "string");
13     updateconfig("./2.php", "name", "admin");
14 */
15 function get_config($file, $ini, $type="string"){
16     if(!file_exists($file)) return false;
17     $str = file_get_contents($file);
18     if ($type=="int"){
19         $config = preg_match("/".preg_quote($ini)."=(.*);/", $str, $res);
20         return $res[1];
21     }
22     else{
23         $config = preg_match("/".preg_quote($ini)."=\"(.*)\";/", $str, $res);
24         if($res[1]==null){ 
25             $config = preg_match("/".preg_quote($ini)."='(.*)';/", $str, $res);
26         }
27         return $res[1];
28     }
29 }
30  
31 function update_config($file, $ini, $value,$type="string"){
32     if(!file_exists($file)) return false;
33     $str = file_get_contents($file);
34     $str2="";
35     if($type=="int"){  
36         $str2 = preg_replace("/".preg_quote($ini)."=(.*);/", $ini."=".$value.";",$str);
37     }
38     else{
39         $str2 = preg_replace("/".preg_quote($ini)."=(.*);/",$ini."=\"".$value."\";",$str);
40     }
41     file_put_contents($file, $str2);
42 }
原文地址:https://www.cnblogs.com/zhwl/p/2579258.html