php reads php configuration file

WBOY
Release: 2016-07-25 09:12:41
Original
2443 people have browsed it
  1. The content of the configuration file my.php is as follows:

    1. $config->installed = true;
    2. $config->debug = false;
    3. $config->requestType = 'GET';
    4. $config->db-> ;host = '16.112.89.126:3306';
    5. $config->db->name = 'test';
    6. $config->db->user = 'root';
    7. $config->db ->password = 'pwd';
    8. $config->db->prefix = 'zt_';
    9. $config->webRoot = getWebRoot();
    10. $config->default->lang = ' zh-cn';
    11. $config->mysqldump = 'D:beanGoumysqlbinmysqldump.exe';
    Copy code

3. Code to read the configuration file:

  1. function get_config($file, $ini, $type="string"){
  2. if(!file_exists($file)) {
  3. echo 'file not exist';
  4. return false;
  5. }
  6. $str = file_get_contents($file);
  7. if ($type=="int"){
  8. $config = preg_match("/".preg_quote($ini)."=(.*);/", $str, $res) ;
  9. return $res[1];
  10. }
  11. else{
  12. // $config = preg_match("/".preg_quote($ini)."="(.*)";/", $str, $res) ;
  13. $config = preg_match("/".preg_quote($ini)."s*=s*"(.*)";/", $str, $res);
  14. if($res[1]== null){
  15. $config = preg_match("/".preg_quote($ini)."s*=s*'(.*)';/", $str, $res);
  16. // $config = preg_match( "/".preg_quote($ini)."='(.*)';/", $str, $res);
  17. }
  18. return $res[1];
  19. }
  20. }
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

  1. //$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,



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!