使用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);
此法参考了http://blog.csdn.net/heiyeshuwu/archive/2004/12/24/227251.aspx
相关推荐
zyyjay 2020-11-09
xuebingnan 2020-11-05
samtrue 2020-11-22
stefan0 2020-11-22
yifangs 2020-10-13
songshijiazuaa 2020-09-24
hebiwtc 2020-09-18
天步 2020-09-17
83911535 2020-11-13
whatsyourname 2020-11-13
zhouyuqi 2020-11-10
Noneyes 2020-11-10
mathchao 2020-10-28
王志龙 2020-10-28
wwwsurfphpseocom 2020-10-28
diskingchuan 2020-10-23
savorTheFlavor 2020-10-23