Home > Backend Development > PHP Tutorial > PHP基础示范:用正则表达式修改配置信息

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

WBOY
Release: 2016-06-13 12:21:07
Original
1145 people have browsed it

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

各php工作原理图:

以下是扯代码时间:

<span style="color: #000000;">php</span><span style="color: #008000;">//</span><span style="color: #008000;">配置文件信息</span>    <span style="color: #008080;">define</span>("HOST","localhost"); <span style="color: #008000;">//</span><span style="color: #008000;">主机名</span>    <span style="color: #008080;">define</span>("USER","rootaaaa");      <span style="color: #008000;">//</span><span style="color: #008000;">用户名</span>    <span style="color: #008080;">define</span>("PASSWORD","root123456");  <span style="color: #008000;">//</span><span style="color: #008000;">密码</span>    <span style="color: #008080;">define</span>("DBNAME","mydb");    <span style="color: #008000;">//</span><span style="color: #008000;">数据库名</span>
Copy after login
dbconfig.php
<span style="color: #000000;">php</span><span style="color: #008000;">//</span><span style="color: #008000;">执行配置文件的修改操作//1.读取配置文件的信息</span><span style="color: #800080;">$info</span> = <span style="color: #008080;">file_get_contents</span>("dbconfig.php"<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">2.对POST遍历,并对配置文件信息进行正则替换</span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$_POST</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$k</span> => <span style="color: #800080;">$v</span><span style="color: #000000;">){    </span><span style="color: #800080;">$info</span>=<span style="color: #008080;">preg_replace</span>("/define\(\"{<span style="color: #800080;">$k</span>}\",\"(.*?)\"\)/","define(\"{<span style="color: #800080;">$k</span>}\",\"{<span style="color: #800080;">$v</span>}\")",<span style="color: #800080;">$info</span><span style="color: #000000;">);}</span><span style="color: #008000;">//</span><span style="color: #008000;">3.将替换后的信息写回到配置文件中</span><span style="color: #008080;">file_put_contents</span>("dbconfig.php",<span style="color: #800080;">$info</span><span style="color: #000000;">);</span><span style="color: #0000ff;">echo</span> "<h2>修改成功!</h2>"<span style="color: #000000;">;</span><span style="color: #0000ff;">echo</span> "<a href="edit.php">返回</a>";
Copy after login
doupdate.php
<span style="color: #000000;">php</span><span style="color: #008000;">//</span><span style="color: #008000;">编辑配置文件</span><span style="color: #800080;">$keyinfo</span>=<span style="color: #0000ff;">array</span>("HOST"=>"主机","USER"=>"用户","PASSWORD"=>"密码","DBNAME"=>"数据库名"<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">1.读取配置文件的信息</span><span style="color: #800080;">$info</span> = <span style="color: #008080;">file_get_contents</span>("dbconfig.php");<span style="color: #008000;">//</span><span style="color: #008000;">将dbconfig.php文件中信息读出并赋值给info变量//2.使用正则表达式解析配置文件的信息</span><span style="color: #008080;">preg_match_all</span>("/define\(\"(.*?)\",\"(.*?)\"\)/",<span style="color: #800080;">$info</span>,<span style="color: #800080;">$a</span>);<span style="color: #008000;">//</span><span style="color: #008000;">将info中的信息解析到a变量中存储//3.遍历解析后的信息,并输出到修改表单中</span><span style="color: #0000ff;">echo</span> "<h2>编辑配置文件</h2>"<span style="color: #000000;">;</span><span style="color: #0000ff;">echo</span> "
Copy after login
";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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template