首页 > 后端开发 > php教程 > PHP基础示范:用正则表达式修改配置信息

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

WBOY
发布: 2016-06-13 12:21:07
原创
1144 人浏览过

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

 

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板