Home > Backend Development > PHP Tutorial > PHP basic example: Use regular expressions to modify configuration information, example regular expression_PHP tutorial

PHP basic example: Use regular expressions to modify configuration information, example regular expression_PHP tutorial

WBOY
Release: 2016-07-13 09:52:10
Original
1087 people have browsed it

PHP basic example: use regular expressions to modify configuration information, example regular expressions

Principle diagram of each PHP working:

The following is the code time:

PHP basic example: Use regular expressions to modify configuration information, example regular expression_PHP tutorialphp //Profile information define("HOST","localhost"); //Host name define("USER","rootaaaa"); //Username define("PASSWORD","root123456"); //Password define("DBNAME","mydb"); //Database name dbconfig.php PHP basic example: Use regular expressions to modify configuration information, example regular expression_PHP tutorialphp //Perform configuration file modification operations //1. Read configuration file information $info = file_get_contents("dbconfig.php"); //2. Traverse the POST and perform regular replacement of the configuration file information foreach($_POST as $k => $v) { $info=preg_replace("/define("{$k}","(.*?)")/","define ("{$k}","{$v}")",$info); } //3. Write the replaced information back to the configuration file file_put_contents("dbconfig.php",$info); echo "

Modification successful!

"; echo "return"; doupdate.php PHP basic example: Use regular expressions to modify configuration information, example regular expression_PHP tutorialphp //Edit configuration file $keyinfo=array("HOST"=>"Host","USER"=>"User","PASSWORD"=>"Password","DBNAME" =>"Database name"); //1. Read configuration file information $info = file_get_contents("dbconfig.php");//Read the information in the dbconfig.php file and assign it to the info variable //2. Use regular expressions to parse configuration file information preg_match_all("/define("(.*?)","(.*?)")/",$info,$a); //Parse the information in info into a variable and store it //3. Traverse the parsed information and output it to the modification form echo "

Edit configuration file

"; echo "
"; foreach($a[1] as $k =>$v) { echo "{$keyinfo[$v]}:

" ; } echo " "; echo ""; echo "
"; edit.php

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1010338.htmlTechArticlePHP basic example: use regular expressions to modify configuration information, example regular expressions and each PHP working principle diagram: The following is Coding time: ?php // Configuration file information define ("HOST","loca...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template