TEL:400-8793-956
当前位置:程序、服务器

PHP配置文件类

提问者: 近期获赞: 浏览人数: 发布时间:2020-12-17 11:50:10

 问:你好!我需要一个正在使用的应用程序的配置文件,因此我首先在php中使用了内置的ini函数,但是编写ini文件并不容易。所以我在这里给自己写了一个类来解析配置文件并编写/编辑配置文件。我计划最终为它编写一些其他有用的函数,但是它确实非常简单,所以我想分享一下。希望能帮助到你!好的,这是课程:

 
class config {
    //this function parses a config file
    public function parse_config($file){
        //fetch the contents of the specified file.
        $content = file_get_contents($file);
        //splits the content into an array by line break
        $lines = explode("\n",$content);
        //define a array to tag every property on.
        $array = array();
        //loops through the array/lines
        foreach ($lines as $value) {
            //if the first character of the line isn't a # (comment character) continue on, if it is it's just ignored.
            if(substr($value,0,1) != "#"){
                //Split the line at = to fetch the property name and value.
                $lineSplit = explode("=",$value);
                //define the property name
                $propName = $lineSplit[0];
                //define the property value
                $propValue = $lineSplit[1];
                //trim whitespace
                $propValue = substr($propValue,0,strlen($propValue)-1);
                //set the property in the array equal to it's value
                $array[$propName] = $propValue;
            }
        }
        return $array;
    }
    //this function writes or edits a config file
    //defining a var quickly
    public function edit_config($property,$equals,$file){
        //fetch content of the file
        $content = file_get_contents($file);
        //splits the content into an array by line break
        $lines = explode("\n",$content);
        //defines a array for new lines, throwing edited one in the mix
        $newLines = array();
        //counter
        $count = 0;
        //loops through the array/lines
        foreach($lines as $value) {
            //length of specified property name
            $propLength = strlen($property);
            //check if the beginning of line is equal to the property name
            if(substr($value,0,$propLength) == $property){
                //if it is add the new value to the new line array
                $newLines[$count] = "$property=$equals";
            }else {
                //if not just add the original line
                $newLines[$count] = $value;
            }
            //increment the count
            $count= $count+1;
        }
        $final;
        //loop through the newLines array, and append it to the final string with a line break
        foreach($newLines as $i){
        //is so extra lines arent added at top of file
            if(!isset($final)){
                $final = $i;
            }else {
                $final .= "\n".$i;
            }
        }
        //write the new file
        $write = fopen($file,"w");
        fwrite($write,$final);
        fclose($write);
    }
}
基本上,#字符是注释字符,必须是该行的第一个空格才能注释掉该行。自由插入换行符,它们在配置文件中都将被忽略。我会给你一个例子。
这是一个伪造的配置文件:
 
#This is a configuration file for some sort of web application!
#This thing does....
banner=this is banner text
#this does...
footer=this text goes in the footer
#All of those are examples
现在可以解析您将执行以下操作:
 
//create an instance of the class of course, include the file if you are in a different on as well obviously.
$class = new config();
//it can be any readable file extension really, I just chose cfg.
//set it to a variable since it outputs a array
$configs = $class->parse_config("thefile.cfg");
echo $configs['banner'];
该代码将this is banner text
立即输出以编辑配置文件,您将执行以下操作:
 
//still with an instance of the class of course
$class = new config();
 
$configs = $class->edit_config("footer","a brand new footer","thefile.cfg");
现在一些解释。第一个参数是将要更改的属性。第二个参数是属性将更改为的参数,第三个参数是文件。
该代码会将其输出到文件中:
 
#This is a configuration file for some sort of web application!
#This thing does....
banner=this is banner text
#this does...
footer=a brand new footer
#All of those are examples
 
 
答:如果您使用file而不是,则file_get_contents默认情况下会得到一个数组。
上一篇: 如何处理验证码?
下一篇: 带有自定义分类标记的Wordpress自定义帖子类型