PHP parse_ini_file函数的应用与扩展操作示例


Posted in PHP onJanuary 07, 2019

本文实例讲述了PHP parse_ini_file函数的应用与扩展操作。分享给大家供大家参考,具体如下:

parse_ini_file($filename, $process_sections = false, $scanner_mode = INI_SCANNER_NORMAL)解析一个配置文件。

filename要解析的文件名;

process_sections设置为true时,得到一个多维数组,包括配置文件中每一节的名称和设置,默认为false;

解析成功返回关联数组,失败返回false。列举一下官网的例子,也引用了官网的扩展实例parse_ini_file_multi()

下面是配置文件内容:

[first_section]
one = 1
two = 2
name = test
[second_section]
path = '/tmp/test'
url = 'http://test.com/login.php'
[third_section]
php_version[] = '5.0'
php_version[] = '5.1'
php_version[] = '5.5'
[dictionary]
foo[debug] = true
foo[path] = /some/path
[fourth_section]
fold1.fold2.fold3 = 'a'
fold1.fold2.fold4 = 'b'
fold1.fold2.fold5 = 'b'

下面是PHP文件内容:

function parse_ini_file_multi($file, $process_sections = false, $scanner_mode = INI_SCANNER_NORMAL){
 $explode_str = '.';
 $escape_char = "'";
 $data = parse_ini_file($file, $process_sections, $scanner_mode);
 if (!$process_sections) {
  $data = array($data);
 }
 foreach ($data as $section_key => $section) {
  foreach($section as $key => $value){
   if(strpos($key, $explode_str)){
    if(substr($key, 0, 1) !== $escape_char){
     $sub_keys = explode($explode_str, $key);
     $subs =& $data[$section_key];
     echo "\r\n".'========='."\r\n";
     print_r($subs);
     print_r($data);
     foreach($sub_keys as $sub_key){
      if (!isset($subs[$sub_key])) {
       $subs[$sub_key] = [];
      }
      $subs =& $subs[$sub_key];
      echo "\r\n".'++++++++'."\r\n";
      print_r($subs);
      print_r($data);
     }
     $subs = $value;
     echo "\r\n".'----------'."\r\n";
     print_r($subs);
     print_r($data);
     unset($data[$section_key][$key]);
    }else{
     $new_key = trim($key, $escape_char);
     $data[$section_key][$new_key] = $value;
     unset($data[$section_key][$key]);
    }
   }
  }
 }
 if (!$process_sections) {
  $data = $data[0];
 }
 return $data;
}
$arr = parse_ini_file('file.ini');
print_r($arr);
echo "\r\n".'================='."\r\n";
$arr = parse_ini_file_multi('file.ini',true);
echo "\r\n".'================='."\r\n";
print_r($arr);

运行结果:

Array ( [one] => 1 [two] => 2 [name] => test [path] => /tmp/test [url] => http://test.com/login.php [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) [foo] => Array ( [debug] => 1 [path] => /some/path ) [fold1.fold2.fold3] => a [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b ) ================= ========= Array ( [fold1.fold2.fold3] => a [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold3] => a [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b ) ) ++++++++ Array ( ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold3] => a [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( ) ) ) ++++++++ Array ( ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold3] => a [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( ) ) ) ) ++++++++ Array ( ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold3] => a [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => Array ( ) ) ) ) ) ---------- aArray ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold3] => a [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a ) ) ) ) ========= Array ( [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a ) ) ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a ) ) ) ) ++++++++ Array ( [fold2] => Array ( [fold3] => a ) ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a ) ) ) ) ++++++++ Array ( [fold3] => a ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a ) ) ) ) ++++++++ Array ( ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a [fold4] => Array ( ) ) ) ) ) ---------- bArray ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a [fold4] => b ) ) ) ) ========= Array ( [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a [fold4] => b ) ) ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a [fold4] => b ) ) ) ) ++++++++ Array ( [fold2] => Array ( [fold3] => a [fold4] => b ) ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a [fold4] => b ) ) ) ) ++++++++ Array ( [fold3] => a [fold4] => b ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a [fold4] => b ) ) ) ) ++++++++ Array ( ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a [fold4] => b [fold5] => Array ( ) ) ) ) ) ---------- bArray ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a [fold4] => b [fold5] => b ) ) ) ) ================= Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1] => Array ( [fold2] => Array ( [fold3] => a [fold4] => b [fold5] => b ) ) ) )

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
php学习 函数 课件
Jun 15 PHP
Admin generator, filters and I18n
Oct 06 PHP
PHP字符串函数系列之nl2br(),在字符串中的每个新行 (\n) 之前插入 HTML 换行符br
Nov 10 PHP
php获取表单中多个同名input元素的值
Mar 20 PHP
PHP图像处理之imagecreate、imagedestroy函数介绍
Nov 19 PHP
php文件扩展名判断及获取文件扩展名的N种方法
Sep 12 PHP
php去除二维数组的重复项方法
Nov 03 PHP
PHP扩展迁移为PHP7扩展兼容性问题记录
Feb 15 PHP
php获取当前url地址的方法小结
Jan 10 PHP
PHP中ltrim()函数的用法与实例讲解
Mar 28 PHP
tp5框架基于ajax实现异步删除图片的方法示例
Feb 10 PHP
详解php中流行的rpc框架
May 29 PHP
PHP一个简单的无需刷新爬虫
Jan 05 #PHP
PHP智能识别收货地址信息实例
Jan 05 #PHP
PHP数字金额转换成中文大写显示
Jan 05 #PHP
PHP yield关键字功能与用法分析
Jan 03 #PHP
PHP获取对象属性的三种方法实例分析
Jan 03 #PHP
PHP获取HTTP body内容的方法
Dec 31 #PHP
php两点地理坐标距离的计算方法
Dec 29 #PHP
You might like
php中引用符号(&)的使用详细介绍
2016/12/06 PHP
不错的asp中显示新闻的功能
2006/10/13 Javascript
认识延迟时间为0的setTimeout
2008/05/16 Javascript
基于Jquery的标签智能验证实现代码
2010/12/27 Javascript
简单漂亮的js弹窗可自由拖拽且兼容大部分浏览器
2013/10/22 Javascript
浅析jQuery1.8的几个小变化
2013/12/10 Javascript
JS中产生标识符方式的演变
2015/06/12 Javascript
jquery ztree实现模糊搜索功能
2016/02/25 Javascript
利用AJAX实现WordPress中的文章列表及评论的分页功能
2016/05/17 Javascript
Knockoutjs 学习系列(二)花式捆绑
2016/06/07 Javascript
js removeChild 方法深入理解
2016/08/16 Javascript
使用Bootstrap Tabs选项卡Ajax加载数据实现
2016/12/23 Javascript
微信小程序 传值取值的几种方法总结
2017/01/16 Javascript
微信小程序 侧滑删除(左滑删除)
2017/05/23 Javascript
Angularjs 事件指令详细整理
2017/07/27 Javascript
js实现canvas图片与img图片的相互转换的示例
2017/08/31 Javascript
[58:54]EG vs RNG 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/18 DOTA
python3编码问题汇总
2016/09/06 Python
Python排序搜索基本算法之冒泡排序实例分析
2017/12/09 Python
Ubuntu下使用python读取doc和docx文档的内容方法
2018/05/08 Python
django中模板的html自动转意方法
2018/05/27 Python
python构造函数init实例方法解析
2020/01/19 Python
Pycharm自动添加文件头注释和函数注释参数的方法
2020/10/23 Python
用Python自动清理电脑内重复文件,只要10行代码(自动脚本)
2021/01/09 Python
基于tensorflow __init__、build 和call的使用小结
2021/02/26 Python
分享全球十款超强HTML5开发工具
2014/05/14 HTML / CSS
Charles & Colvard官网:美国莫桑石品牌
2019/06/05 全球购物
巴西网上药店:Drogaria Araujo
2021/01/06 全球购物
阿里巴巴Oracle DBA笔试题答案-备份恢复类
2013/11/20 面试题
领导走群众路线整改措施思想汇报
2014/10/12 职场文书
2014年乡镇纪委工作总结
2014/12/19 职场文书
公司放假通知怎么写
2015/04/15 职场文书
创业计划书之便利店
2019/09/05 职场文书
Java方法重载和方法重写的区别到底在哪?
2021/06/11 Java/Android
使用ICOM IC-R9500接收机同时测评十台收音机中波接收性能
2022/05/10 无线电
详解Spring Bean的配置方式与实例化
2022/06/10 Java/Android