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
//Profile information
define("HOST","localhost");
//Host name
define("USER","rootaaaa");
//Username
define("PASSWORD","root123456");
//Password
define("DBNAME","mydb");
//Database name
dbconfig.php
php
//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
//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 "
";
edit.php
http://www.bkjia.com/PHPjc/1010338.htmlwww.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...