Code for PHP to read and modify custom configuration files

Source: Internet
Author: User
    1. /**
    2. Desc: Configuration File
    3. link:bbs.it-home.org
    4. Date:2013/2/24
    5. */
    6. $name = "admin";//kkkk
    7. $BB = ' 234 ';
    8. $db = 4561321;
    9. $KKK = "admin";
    10. ?>
Copy Code

Function definition: Profile data value acquisition: function GetConfig ($file, $ini, $type = "string") Profile data Entry update: function Updateconfig ($file, $ini, $value, $ Type= "string") Call Mode:

  1. GetConfig ("./2.php", "BB");//

  2. Updateconfig ("./2.php", "KKK", "admin");

  3. Gets the profile data value.

  4. When the default does not have a third argument, reads the contents of the extracted "in or" from the string
  5. If there is a third argument, int is treated as a numeric int.
  6. function GetConfig ($file, $ini, $type = "string")
  7. {
  8. if ($type = = "int")
  9. {
  10. $str = file_get_contents ($file);
  11. $config = Preg_match ("/". $ini. "= (. *);/", $str, $res);
  12. Return $res [1];
  13. }
  14. Else
  15. {
  16. $str = file_get_contents ($file);
  17. $config = Preg_match ("/". $ini. "=\" (. *) \ ";/", $str, $res);
  18. if ($res [1]==null)
  19. {
  20. $config = Preg_match ("/". $ini. "= ' (. *) ';/", $str, $res);
  21. }
  22. Return $res [1];
  23. }
  24. }

  25. Configuration file Data Item update

  26. The contents of the extract ' in or ' are read by string when there is no fourth parameter by default
  27. If there is a fourth argument, int is treated as a numeric int.
  28. function Updateconfig ($file, $ini, $value, $type = "string")
  29. {
  30. $str = file_get_contents ($file);
  31. $str 2 = "";
  32. if ($type = = "int")
  33. {
  34. $str 2 = preg_replace ("/". $ini. "= (. *);/", $ini. " =" . $value. ";", $str);
  35. }
  36. Else
  37. {
  38. $str 2 = preg_replace ("/". $ini. "= (. *);/", $ini. " =\"" . $value. "\"; ", $str);
  39. }
  40. File_put_contents ($file, $str 2);
  41. }

  42. Echo GetConfig ("./2.php", "BB", "string");

  43. GetConfig ("./2.php", "BB");//
  44. Updateconfig ("./2.php", "KKK", "admin");
  45. echo "
    ". GetConfig ("./2.php "," Name "," string ");
  46. ?>

Copy Code

The following is an improved version

  1. Improve the improved version

  2. /**
  3. * link:bbs.it-home.org
  4. * DATE:2013/2/24
  5. * Configuration file operation (queried and modified)
  6. * When the default does not have a third parameter, read the contents of the extract "in or" by string
  7. * If there is a third argument when int is treated as a numeric int.
  8. * Call Demo
  9. $name = "admin";//kkkk
  10. $BB = ' 234 ';

  11. $BB =getconfig ("./2.php", "BB", "string");

  12. Updateconfig ("./2.php", "name", "admin");
  13. */
  14. function Get_config ($file, $ini, $type = "string") {
  15. if (!file_exists ($file)) return false;
  16. $str = file_get_contents ($file);
  17. if ($type = = "int") {
  18. $config = Preg_match ("/". Preg_quote ($ini). " = (. *);/", $str, $res);
  19. return $res [1];
  20. }
  21. else{
  22. $config = Preg_match ("/". Preg_quote ($ini). " =\ "(. *) \";/", $str, $res);
  23. if ($res [1]==null) {
  24. $config = Preg_match ("/". Preg_quote ($ini). " = ' (. *) ';/", $str, $res);
  25. }
  26. return $res [1];
  27. }
  28. }

  29. function Update_config ($file, $ini, $value, $type = "string") {

  30. if (!file_exists ($file)) return false;
  31. $str = file_get_contents ($file);
  32. $str 2 = "";
  33. if ($type = = "int") {
  34. $str 2 = preg_replace ("/". Preg_quote ($ini). " = (. *);/", $ini." = ". $value."; ", $str);
  35. }
  36. else{
  37. $str 2 = preg_replace ("/". Preg_quote ($ini). " = (. *);/", $ini." =\ "". $value. " \ ";", $str);
  38. }
  39. File_put_contents ($file, $str 2);
  40. }
  41. ?>

Copy CodeYou may be interested in the article: PHP des encryption decrypted Code One example PHP uses 3DES encrypted code (compatible with. NET)
  • 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.