Home > php教程 > php手册 > PHP基础示例:用正则表达式修改配置信息,示例正则表达式

PHP基础示例:用正则表达式修改配置信息,示例正则表达式

WBOY
Release: 2016-06-13 09:02:55
Original
1321 people have browsed it

PHP基础示例:用正则表达式修改配置信息,示例正则表达式

各php工作原理图:

以下是扯代码时间:

php //配置文件信息 define("HOST","localhost"); //主机名 define("USER","rootaaaa"); //用户名 define("PASSWORD","root123456"); //密码 define("DBNAME","mydb"); //数据库名 dbconfig.php php //执行配置文件的修改操作 //1.读取配置文件的信息 $info = file_get_contents("dbconfig.php"); //2.对POST遍历,并对配置文件信息进行正则替换 foreach($_POST as $k => $v) { $info=preg_replace("/define\(\"{$k}\",\"(.*?)\"\)/","define(\"{$k}\",\"{$v}\")",$info); } //3.将替换后的信息写回到配置文件中 file_put_contents("dbconfig.php",$info); echo "

修改成功!

"; echo "返回"; doupdate.php php //编辑配置文件 $keyinfo=array("HOST"=>"主机","USER"=>"用户","PASSWORD"=>"密码","DBNAME"=>"数据库名"); //1.读取配置文件的信息 $info = file_get_contents("dbconfig.php");//将dbconfig.php文件中信息读出并赋值给info变量 //2.使用正则表达式解析配置文件的信息 preg_match_all("/define\(\"(.*?)\",\"(.*?)\"\)/",$info,$a);//将info中的信息解析到a变量中存储 //3.遍历解析后的信息,并输出到修改表单中 echo "

编辑配置文件

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

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

 

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template