Rumah >pembangunan bahagian belakang >tutorial php >PHP基础示范:用正则表达式修改配置信息

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

WBOY
WBOYasal
2016-06-13 12:21:071146semak imbas

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>
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>";
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> "
";foreach($a[1] as $k =>$v){ echo "{$keyinfo[$v]}:

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

 

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn