-
The content of the configuration file my.php is as follows:
- $config->installed = true;
- $config->debug = false;
- $config->requestType = 'GET';
- $config->db-> ;host = '16.112.89.126:3306';
- $config->db->name = 'test';
- $config->db->user = 'root';
- $config->db ->password = 'pwd';
- $config->db->prefix = 'zt_';
- $config->webRoot = getWebRoot();
- $config->default->lang = ' zh-cn';
- $config->mysqldump = 'D:beanGoumysqlbinmysqldump.exe';
Copy code
3. Code to read the configuration file:
- function get_config($file, $ini, $type="string"){
- if(!file_exists($file)) {
- echo 'file not exist';
- 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) ;
- $config = preg_match("/".preg_quote($ini)."s*=s*"(.*)";/", $str, $res);
- if($res[1]== null){
- $config = preg_match("/".preg_quote($ini)."s*=s*'(.*)';/", $str, $res);
- // $config = preg_match( "/".preg_quote($ini)."='(.*)';/", $str, $res);
- }
- return $res[1];
- }
- }
Copy code
The code corresponding to the two lines of comments is from the Internet. Because there are spaces, it cannot be read. I made some modifications myself. After the modification, it can be read even if there are spaces.
Note: If the configuration file my.php has comments, if there is something like this
- //$config->db->host = '16.112.89.126:3306';
Copy the code
, this method will read the comment content, so if it is useless, the last Easy to delete,
|