首頁  >  文章  >  後端開發  >  php讀取php設定檔

php讀取php設定檔

WBOY
WBOY原創
2016-07-25 09:12:412413瀏覽
  1. 配置文件my.php内容如下:

    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';
    复制代码

3.读取该配置文件的代码:

  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. }
复制代码

两行注释对应的代码是网上的,因为有空格读不到,我自己做了点修改,修改后,有空格同样能读到。

注意:如果配置文件my.php有注释,如果有类似这样的

  1. //$config->db->host = '16.112.89.126:3306';
复制代码

,该方法会读到注释内容,所以,如果没用的话,最好删掉、



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn