日期:2014-05-17 浏览次数:20397 次
$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);