>  기사  >  백엔드 개발  >  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:\beanGou\mysql\bin\mysqldump.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으로 문의하세요.