服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

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

2019-07-07 16:46徐三少北 PHP教程

这篇文章主要介绍了PHP parse_ini_file函数的应用与扩展操作,结合实例形式分析了php扩展parse_ini_file函数解析配置文件相关操作技巧,需要的朋友可以参考下

本文实例讲述了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删除数组中特定元素的两种方法

    PHP删除数组中特定元素的两种方法

    今天小编就为大家分享一篇关于PHP删除数组中特定元素的两种方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧...

    炫酷飞扬3152019-06-09
  • PHP教程PHP-FPM 的管理和配置详解

    PHP-FPM 的管理和配置详解

    这篇文章主要介绍了PHP-FPM 的管理和配置详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    Donne2842019-06-12
  • PHP教程Windows上php5.6操作mongodb数据库示例【配置、连接、获取实例】

    Windows上php5.6操作mongodb数据库示例【配置、连接、获取实例】

    这篇文章主要介绍了Windows上php5.6操作mongodb数据库,结合实例形式简单分析了Windows平台上使用php5.6针对MongoDB数据库的配置、连接、获取实例等相关操作技巧,需要的朋友可以参考下...

    Sulley0072072019-06-16
  • PHP教程实例介绍PHP中zip_open()函数用法

    实例介绍PHP中zip_open()函数用法

    在本篇内容里小编给大家分享了关于PHP中zip_open()函数用法的相关知识点,有需要的朋友们跟着学习下。...

    laozhang2612019-06-14
  • PHP教程PHP DB 数据库连接类定义与用法示例

    PHP DB 数据库连接类定义与用法示例

    这篇文章主要介绍了PHP DB 数据库连接类定义与用法,涉及php基于mysqli针对数据库的连接、增删改查等常见操作封装与使用技巧,需要的朋友可以参考下...

    moTzxx4962019-06-02
  • PHP教程php-fpm中max_children的配置

    php-fpm中max_children的配置

    今天小编就为大家分享一篇关于php-fpm中max_children的配置,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧...

    CODETC3242019-05-30
  • PHP教程PHP实现字符串大小写转函数的功能实例

    PHP实现字符串大小写转函数的功能实例

    这篇文章主要给大家介绍了关于利用PHP如何实现字符串大小写转函数功能的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友一起来看看啊...

    写PHP的老王4642019-06-19
  • PHP教程PDO::errorCode讲解

    PDO::errorCode讲解

    今天小编就为大家分享一篇关于PDO::errorCode讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧...

    php参考手册1082019-06-26