PHP configuration file settings

Source: Internet
Author: User
The method variable $ config for setting the configuration file in PHP is the configuration information we submitted and stored in arrays. PHP configuration file settings

The variable $ config is the configuration information we submitted and stored as an array.

function set_config($config){        if(!is_array($config)) return FALSE;        $configfile = CONF_PATH.'config.php';        if(!is_writable($configfile)) return('Please chmod ./config.inc.php to 0777 !');        $pattern = $replacement = array();        $str = file_get_contents($configfile);        foreach($config as $k=>$v){            $pattern[$k] = "/['\"]".strtoupper($k)."['\"]\s*=>\s*([']?)[^']*([']?)/is";            $replacement[$k] = "'".strtoupper($k)."'=> \${1}".$v."\${2}";        }                 $str = preg_replace($pattern, $replacement, $str);        return file_put_contents($configfile, $str);    }


The above $ configfile is the complete path of the file, that is, the absolute path.

Use the file_get_contents file to obtain all the information in the configuration file.

Use the preg_replace method to replace the submitted configuration information with the original configuration information.

Replace all modified information with file_put_contents.


The above method can be used not only to modify configuration information, but also to modify any file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.