php动态修改配置文件

<?php
define("host","localhost");
define("port","3306");
define("user","root");
define("pwd","root2");
define("dbname","text23");
<?php
//1.读取配置文件的信息
$info = file_get_contents("dbcongfig.php");
$keyinfo = array("host"=>"主机","port"=>"端口","user"=>"账号","pwd"=>"密码","dbname"=>"数据库",);
//2.使用正则表达式解析配置文件的信息 .任意字符  * 任意数量 ?拒绝贪婪匹配
preg_match_all("/define("(.*?)","(.*?)")/",$info,$temp);

echo "<h1>编辑配置文件</h1>";
echo "<form action='upt.php' method='post' >";
foreach($temp[1] as $k=>$v){
    echo "$keyinfo[$v]:<input type='text' name='{$v}' value='{$temp[2][$k]}'  /><br/><br/>";
}
echo "<input type='submit' value='修改' />&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<input type='reset' value='重置' />";
echo "</form>";
<?php
//1.获取配置信息
$info = file_get_contents("dbcongfig.php");
//2.对配置文件进行正则替换
foreach($_POST as $k=>$v){
    $info = preg_replace("/define("{$k}",".*?")/","define("{$k}","{$v}")", $info);
}
file_put_contents("dbcongfig.php", $info);
echo "<script>alert('修改成功');</script>";
echo "<script>self.location='file_get_content.php'</script>";
原文地址:https://www.cnblogs.com/lzy007/p/6220490.html