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

yipeiwu_com4年前PHP代码库

本文实例讲述了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+mysql数据库操作入门教程》、《PHP网络编程技巧总结》及《php常见数据库操作技巧汇总

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

相关文章

PHP5.5新特性之yield理解与用法实例分析

本文实例讲述了PHP5.5新特性之yield理解与用法。分享给大家供大家参考,具体如下: yield生成器是php5.5之后出现的,yield提供了一种更容易的方法来实现简单的迭代对象,...

PHP SPL使用方法和他的威力

SPL,PHP 标准库(Standard PHP Library) ,此从 PHP 5.0 起内置的组件和接口,并且从 PHP5.3 已逐渐的成熟。SPL 其实在所有的 PHP5 开发环...

php运行时动态创建函数的方法

本文实例讲述了php运行时动态创建函数的方法。分享给大家供大家参考。具体分析如下: 一般的语言函数必须定义了在运行,而php支持在运行时动态创建函数,下面是一个简单的范例,在运动时根据不...

php实现倒计时效果

php实现倒计时效果

现在很多的团购网站上都有剩余时间的显示。显示剩余时间可以使用Javascript来实现,但是我们会发现使用Javascript来实现的话不安全,因为Javascript获取的是客户端的时...

phpfpm的作用和用法

PHP-FPM(FastCGI Process Manager:FastCGI进程管理器)是一个PHPFastCGI管理器,对于PHP 5.3.3之前的php来说,是一个补丁包 ,旨在将...