运用PHP对配置文件进行修改

WBOY
Release: 2016-06-13 10:38:33
Original
968 people have browsed it

使用PHP对配置文件进行修改
http://www.phpweblog.net/kiyone/archive/2007/07/04/1432.html


想修改配置文件aaa.conf,内容如下:
[username]
kiyone

[password]
123

[class]
chass1
我想要修改[password]下面的123这行密码。方案采用把这两行替换成空,然后在重新把这两行写在配置文件的结尾。

$fp = fopen("aaa.conf", 'r');    $configfile = fread($fp, filesize("aaa.conf"));    fclose($fp);    //通过正则替换来做    $configfile = preg_replace("/\\n\[password\](.+?)\\n/is", "", $configfile);//本只需匹配[password]到下一空行之间的内容,只需写成/\[password\](.+?)\\n/is就行了,但是我想把这行前面的空行也去掉,所以在前面加了个\n    //把文件重新写回原来的地方    $fp = fopen("aaa.conf", 'w');    fwrite($fp, trim($configfile));    fclose($fp);    //在文件最后加入新的password两行     $newpassword = "456";    $filename="aaa.conf";//定义操作文件    $fcontent = file($filename); //file()把整个文件读入一个数组中    $fp  = fopen("$filename","a");    $str = "\n\n[password]\n$newpassword";    fwrite($fp, $str);
Copy after login



此法参考了http://blog.csdn.net/heiyeshuwu/archive/2004/12/24/227251.aspx
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