php读取php配置文件

php教程评论647 views阅读模式
  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);

复制代码

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

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

复制代码

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

企鹅博客
  • 本文由 发表于 2020年7月19日 22:10:59
  • 转载请务必保留本文链接:https://www.qieseo.com/319594.html

发表评论