Use PHP to modify the configuration file

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.