php讀取php配置文件
配置文件my.php內容如下:
<?php $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:\beanGou\mysql\bin\mysqldump.exe';
3.讀取該配置文件的代碼:
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]; } }
兩行注釋對應的代碼是網上的,因為有空格讀不到,我自己做了點修改,修改后,有空格同樣能讀到。
注意:如果配置文件my.php有注釋,如果有類似這樣的
//$config->db->host = '16.112.89.126:3306';
,該方法會讀到注釋內容,所以,如果沒用的話,最好刪掉、
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!