欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-30 20:35:28
本文实例讲述了php parse_ini_file函数的应用与扩展操作。分享给大家供大家参考,具体如下: parse_ini_file($filename, $proce...

本文实例讲述了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程序设计有所帮助。