php读取与修改自定义配置文件的代码

php中文网
发布: 2016-07-25 09:04:42
原创
1424人浏览过
  1. /**
  2. desc:配置文件
  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. ?>
复制代码

函数定义: 配置文件数据值获取:function getconfig($file, $ini, $type="string") 配置文件数据项更新:function updateconfig($file, $ini, $value,$type="string") 调用方式:

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

  2. updateconfig("./2.php", "kkk", "admin");
  3. //配置文件数据值获取。

  4. //默认没有第三个参数时,按照字符串读取提取''中或""中的内容
  5. //如果有第三个参数时为int时按照数字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. //配置文件数据项更新

  26. //默认没有第四个参数时,按照字符串读取提取''中或""中的内容
  27. //如果有第四个参数时为int时按照数字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. ?>
复制代码

以下是改进后的版本

  1. //完善改进版

    图改改
    图改改

    在线修改图片文字

    图改改 455
    查看详情 图改改
  2. /**
  3. * link:bbs.it-home.org
  4. * date:2013/2/24
  5. * 配置文件操作(查询了与修改)
  6. * 默认没有第三个参数时,按照字符串读取提取''中或""中的内容
  7. * 如果有第三个参数时为int时按照数字int处理。
  8. *调用demo
  9. $name="admin";//kkkk
  10. $bb='234';

    立即学习PHP免费学习笔记(深入)”;

  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. ?>
复制代码
您可能感兴趣的文章: php DES加密解密的代码一例 php使用3des加密的代码(兼容.net)


PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号