在php中怎样修改配置文件?
在php中修改配置文件的方法:
一、先修改引导页
<?phpinclude("dbconfig.php");?><form action="edit.php" method="post"> <br /> 端口:<input type="text" name="db_host" value="<?php echo db_host;?>" /><br /> 用户名:<input type="text" name="db_user" value="<?php echo db_user;?>" /><br /> 密码:<input type="text" name="db_pass" value="<?php echo db_pass;?>" /><br /> 数据库名:<input type="text" name="db_name" value="<?php echo db_name;?>" /><br /> <input type="submit" value="修改" /></form>
二,修改页面
<?php//获取配置文件的内容$string = file_get_contents('config.php');foreach ($_post as $key=>$value) { //正则匹配对应的一行 $old_inf = "/define\('$key','.*?'\);/"; //要修改成的数据 $new_inf = "define('$key','$value');"; //替换 $string = preg_replace($old_inf,$new_inf,$string);}//写入配置文件file_put_contents('config.php',$string);echo "修改成功";
推荐教程:《php视频教程》
以上就是在php中怎样修改配置文件?的详细内容。