Use PHP to modify the configuration file www.phpweblog.netkiyonearchive200707041432.html to modify the aaa. conf file. the content is as follows: [username] kiyone [password] 123 [class] chass1 I want to modify the 123 line password under [password. The solution replaces the two lines and uses PHP to modify the configuration file.
Http://www.phpweblog.net/kiyone/archive/2007/07/04/1432.html
To modify the configuration file aaa. conf, the content is as follows:
[Username]
Kiyone
[Password]
123
[Class]
Chass1
I want to change the 123 line password under [password. The solution replaces the two rows with null, and then re-writes the two rows at the end of the configuration file.
$ Fp = fopen ("aaa. conf ", 'r'); $ configfile = fread ($ fp, filesize (" aaa. conf "); fclose ($ fp); // use regular expression replacement to replace $ configfile = preg_replace (" // \ n \ [password \] (. + ?) \ N/is "," ", $ configfile); // you only need to match the content between the [password] and the next empty line. you only need to write it as/\ [password \] (. + ?) \ N/is, but I want to remove the blank lines in front of this line, therefore, add \ n // to the front to re-write the file back to the original place $ fp = fopen ("aaa. conf ", 'w'); fwrite ($ fp, trim ($ configfile); fclose ($ fp ); // add the new password line $ newpassword = "456"; $ filename = "aaa. conf "; // define the operation file $ fcontent = file ($ filename); // file () read the entire file into an array $ fp = fopen (" $ filename ", "a"); $ str = "\ n [password] \ n $ newpassword"; fwrite ($ fp, $ str );
This method references http://blog.csdn.net/heiyeshuwu/archive/2004/12/24/227251.aspx