Php reads and modifies the code of the custom configuration file

Source: Internet
Author: User
Php reads and modifies the code of the custom configuration file

  1. /**
  2. Desc: configuration file
  3. Link: bbs.it-home.org
  4. Date:
  5. */
  6. $ Name = "admin"; // kkkk
  7. $ Bb = '000000 ';
  8. $ Db = 4561321;
  9. $ Kkk = "admin ";
  10. ?>

Function definition: get the data value of the configuration file: function getconfig ($ file, $ ini, $ type = "string") configuration file data item update: function updateconfig ($ file, $ ini, $ value, $ type = "string") call method:

  1. Getconfig ("./2.php"," bb ");//

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

  3. // Obtain the configuration file data value.

  4. // If no third parameter exists by default, extract the content in ''or" "according to the string
  5. // If there is a third parameter, int is used.
  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. // Update configuration file data items

  26. // If no fourth parameter exists by default, extract the content in ''or" "according to the string
  27. // If there is a fourth parameter, int is used.
  28. Function updateconfig ($ file, $ ini, $ value, $ type = "string ")
  29. {
  30. $ Str = file_get_contents ($ file );
  31. $ Str2 = "";
  32. If ($ type = "int ")
  33. {
  34. $ Str2 = preg_replace ("/". $ ini. "= (. *);/", $ ini. "=". $ value. ";", $ str );
  35. }
  36. Else
  37. {
  38. $ Str2 = preg_replace ("/". $ ini. "= (. *);/", $ ini. "= \"". $ value. "\"; ", $ str );
  39. }
  40. File_put_contents ($ file, $ str2 );
  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. ?>

The following are the improved versions.

  1. // Complete the ultimate version

  2. /**
  3. * Link: bbs.it-home.org
  4. * Date:
  5. * Configuration file operations (query and modification)
  6. * If no third parameter is provided by default, the content in ''or" "is extracted based on the string.
  7. * If there is a third parameter, int is used.
  8. * Call demo
  9. $ Name = "admin"; // kkkk
  10. $ Bb = '000000 ';

  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. $ Str2 = "";
  33. If ($ type = "int "){
  34. $ Str2 = preg_replace ("/". preg_quote ($ ini ). "= (. *);/", $ ini. "= ". $ value. ";", $ str );
  35. }
  36. Else {
  37. $ Str2 = preg_replace ("/". preg_quote ($ ini ). "= (. *);/", $ ini. "= \"". $ value. "\"; ", $ str );
  38. }
  39. File_put_contents ($ file, $ str2 );
  40. }
  41. ?>

Articles you may be interested in: php DES encryption and decryption code example php uses 3des encryption 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.