设为首页收藏本站淘宝杂货铺

从F到0 - From F to 0

 找回密码
 注册已关闭
搜索
查看: 3765|回复: 9
收起左侧

[PHP/ASP/JSP] PHP7实现INI格式配置项文件读写全操作 读配置项/写配置项/取配置节名

[复制链接]
发表于 2019-3-25 10:39:00 | 显示全部楼层 |阅读模式


  1. <?php
  2. /*
  3. 本程序为读写任意ini格式文件,与 php.ini 没有任何关系。
  4. */

  5. //将 parse_ini_file 返回的二维数组重组成ini文本
  6. function array_pack_ini($ini_array){
  7.         $return = "";
  8.         foreach($ini_array as $node => $node_data){
  9.                 $return.="[".$node."]\r\n";
  10.                 foreach($node_data as $key => $value){
  11.                         $return.=$key."="."$value"."\r\n";
  12.                 }
  13.         }
  14.         return $return;
  15. }

  16. //读配置项 参数1:文件名 参数2:节名称 参数3:配置项名称 参数4:默认文本(可以留空)
  17. function GetKeyText($file,$node,$key,$value=""){        
  18.         return  parse_ini_file($file,1)[$node][$key] ?? $value;
  19. }

  20. //写配置项 参数1:文件名 参数2:节名称 参数3:配置项名称 参数4:欲写入值
  21. function SetKeyText($file,$node,$key,$value){
  22.         $ini_array = parse_ini_file($file,1);
  23.         $ini_array[$node][$key] = $value;
  24.         return file_put_contents($file,array_pack_ini($ini_array));
  25. }

  26. //取配置节名 参数1:文件名
  27. function GetSectionNames($file){
  28.         $return = array();
  29.         foreach(parse_ini_file($file,1) as $node => $node_data){
  30.                 array_push($return,$node);
  31.         }
  32.         return $return;

  33. }
  34. echo GetKeyText("ini.ini","abc","bbc","Err");
  35. SetKeyText("ini.ini","zzz","yyy","abc");
  36. SetKeyText("ini.ini","zzz","fff","def");
  37. SetKeyText("ini.ini","abc","bbc","@@@");
  38. print_r(GetSectionNames("ini.ini"));

  39. ?>



复制代码

相关帖子

发表于 2019-4-9 19:22:39 来自手机 | 显示全部楼层
222 发表于 2019-3-26 10:37
路过一下

你也卡了这么多帖子
发表于 2019-4-14 09:03:20 来自手机 | 显示全部楼层
小清新 发表于 2019-4-9 19:22
你也卡了这么多帖子

服务器的问题,目前已解决
您需要登录后才可以回帖 登录 | 注册已关闭

本版积分规则

QQ|手机版|Archiver|从F到0 ( 蒙ICP备17002595号-1 )
蒙公网安备15010402000325号

腾讯云安全认证

GMT+8, 2024-4-27 10:15 , Processed in 1.685097 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表