parse_ini_file()

1. 定义

该函数解析一个ini配置文件。 成功时返回该配置文件的项的关联数组,失败则返回FALSE。

注意,一些保留字不允许作为配置文件中的键名!

2. 语法

parse_ini_file ( string $filepath [, bool $process_sections = false [, int $scanner_mode = INI_SCANNER_NORMAL ]] ) : array

3. 参数说明

参数 可选性 数据类型 描述
$filepath 必需 字符串 ini配置文件路径
$process_sections 可选 布尔值 可选值,默认为false,如果设为true,则返回一个多维数组
$scanner_mode 可选 整型或常量 可选值

其中$scanner_mode有两个可选值:

  • INI_SCANNER_NORMAL:默认值,默认解析选项值
  • INI_SCANNER_RAW:不解析选项值

4. 示例

<?php

// parse_ini_file()
// 解析一个`ini`配置文件

file_put_contents(__DIR__.'/test.ini', "[extensions]
;extension=php_pdo_oci.dll
;extension=php_pdo_odbc.dll
;extension=php_pdo_pgsql.dll
extension=php_mongodb.dll
extension=php_redis.dll
extension=php_pthreads.dll
zend_extension=php_opcache.dll

[urls]
网站=https://www.jisudev.com/
域名=极速教程");

$filepath = __DIR__.'/test.ini';
$res = parse_ini_file($filepath);
var_dump($res);
/* 输出:
array(4) {
  ["extension"]=>
  string(16) "php_pthreads.dll"
  ["zend_extension"]=>
  string(15) "php_opcache.dll"
  ["网站"]=>
  string(24) "https://www.jisudev.com/"
  ["域名"]=>
  string(12) "极速教程"
}
*/

$res = parse_ini_file($filepath, true);
var_dump($res);
/* 输出:
array(2) {
  ["extensions"]=>
  array(2) {
    ["extension"]=>
    string(16) "php_pthreads.dll"
    ["zend_extension"]=>
    string(15) "php_opcache.dll"
  }
  ["urls"]=>
  array(2) {
    ["网站"]=>
    string(24) "https://www.jisudev.com/"
    ["域名"]=>
    string(12) "极速教程"
  }
}
*/

5. 延展阅读