Home > Backend Development > PHP Tutorial > PHP code for reading and modifying custom configuration files

PHP code for reading and modifying custom configuration files

WBOY
Release: 2016-07-25 09:04:42
Original
1320 people have browsed it
  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: Get configuration file data value: function getconfig($file, $ini, $type="string") Configuration file data item update: function updateconfig($file, $ini, $value,$type="string") Calling method:

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

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

  3. //Get configuration file data value.

  4. //By default, when there is no third parameter, read and extract the content in '' or "" according to the string.
  5. //If there is a third parameter, it is int, and it is processed as a digital 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. //By default, when there is no fourth parameter, read and extract the content in '' or "" according to the string
  27. //If there is a fourth parameter and it is int, it will be processed as a digital int.
  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. ?>

Copy code

The following is the improved version

  1. //Perfect and improved version
  2. /**
  3. * link: bbs.it-home.org
  4. * date: 2013/2/24
  5. * Configuration file operation (query and modification)
  6. * By default, when there is no third parameter, read and extract '' according to the string Or the content in ""
  7. * If there is a third parameter that is an int, it will be processed as a numerical 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. $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. ?>

Copy code
Articles you may be interested in: An example of php DES encryption and decryption code php uses 3des encrypted code (compatible with .net)


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template