PHP Basic Example: Modifying configuration information with regular expressions

Source: Internet
Author: User
Each PHP working principle diagram:

Here is the time to pull the code:

 
  PHP/ /config file information define// hostname define("USER", "rootaaaa");      // User name Define ("PASSWORD", "root123456");  // Password Define ("DBNAME", "mydb");    // Database name

dbconfig.php

 
  PHP// Execute configuration file modification operation//1. Read the configuration file information $infofile_get_contents("dbconfig.php"); // 2. Post traversal, and regular substitution of profile information foreach ($_postas$k$v) {    $info=preg_replace ("/define\ (\" {$k}\ ", \" (. *?) \ "\)/", "define (\" {$k}\ ", \" {$v}\ ")",$info);} // 3. Write the replacement information back into the configuration file file_put_contents ("dbconfig.php",$info); Echo "

Modified successfully!

"; Echo "Return";

doupdate.php

 
  PHP// Edit configuration file $keyinfo=array("host" + "hosts", "User" + "users", "PASSWORD" and "=" Password "," DBNAME "=" database name "); // 1. Read the configuration file information $info file_get_contents ("dbconfig.php"); // the information in the dbconfig.php file is read out and assigned to the info variable//2. Using regular expressions To resolve configuration file information Preg_match_all ("/define\" (. *?) \",\"(.*?) \ "\)/",$info,$a); // parses the information in info into the A variable to store//3. Iterates through the parsed information and outputs it to the modification form Echo "

Editing a configuration file

"; Echo "";

edit.php

The above describes the PHP basic example: using regular expressions to modify configuration information, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.