如何在PHP中使用parse_ini_file函数

发布时间:2021-03-15 17:53:50 作者:Leah
来源:亿速云 阅读:205

如何在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中使用parse_ini_file函数的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. parse_ini_file()函数
  2. metaphone()函数如何在php中使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php parse ini file

上一篇:怎么在php中使用strftime函数

下一篇:如何在php中使用可变函数

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》