首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

parse_ini_file

(PHP 4, PHP 5, PHP 7)

parse_ini_file - 解析配置文件

描述

代码语言:javascript
复制
array parse_ini_file ( string $filename [, bool $process_sections = false [, int $scanner_mode = INI_SCANNER_NORMAL ]] )

parse_ini_file()将加载到指定的ini文件中filename,并将其中的设置返回到关联数组中。

ini文件的结构与php.ini相同。

参数

filename

正在解析的ini文件的文件名。

process_sections

通过设置process_sections参数TRUE,您将得到一个多维数组,并且包含部分名称和设置。process_sectionsis 的默认值是FALSE

scanner_mode

可以是INI_SCANNER_NORMAL(默认)或INI_SCANNER_RAW。如果INI_SCANNER_RAW提供,则不会分析选项值。

至于PHP 5.6.1也可以指定为INI_SCANNER_TYPED。在此模式下,布尔型,空和整数类型在可能的情况下被保留。将字符串值“true”“on”“yes”转换为TRUE“false”“off”“no”“none”被认为是FALSE“null”转换为NULL键入模式。而且,如果可能的话,所有的数字字符串都被转换为整数类型。

返回值

设置在成功时以关联数组形式返回,并在失败时返回FALSE

Changelog

版本

描述

7.0.0

哈希标记(#)不再被视为注释。

5.6.1

增加了新的INI_SCANNER_TYPED模式。

5.3.0

增加了可选的scanner_mode参数。单引号现在可以用于变量赋值。散列标记(#)不应再用作注释,并且如果使用则会抛出弃用警告。

5.2.7

在语法错误时,此函数将返回FALSE而不是空数组。

5.2.4

由数字组成的键和片段名称现在被评估为PHP整数,因此以0开头的数字被评估为八进制数,而以0x开头的数字评估为十六进制数。

5.0.0

用双引号括起来的值可以包含新行。

4.2.1

此功能现在受安全模式和open_basedir的影响。

示例

Example #1 Contents of sample.ini

代码语言:javascript
复制
; This is a sample configuration file
; Comments start with ';', as in php.ini

[first_section]
one = 1
five = 5
animal = BIRD

[second_section]
path = "/usr/local/bin"
URL = "http://www.example.com/~username"

[third_section]
phpversion[] = "5.0"
phpversion[] = "5.1"
phpversion[] = "5.2"
phpversion[] = "5.3"

urls[svn] = "http://svn.php.net"
urls[git] = "http://git.php.net"

Example #2 parse_ini_file() example

常量也可以在ini文件中进行解析,所以如果在运行parse_ini_file()之前将常量定义为ini值,它将会被集成到结果中。只有ini值被评估。例如:

代码语言:javascript
复制
<?php

define('BIRD', 'Dodo bird');

// Parse without sections
$ini_array = parse_ini_file("sample.ini");
print_r($ini_array);

// Parse with sections
$ini_array = parse_ini_file("sample.ini", true);
print_r($ini_array);

?>

上面的例子会输出类似于:

代码语言:javascript
复制
Array
(
    [one] => 1
    [five] => 5
    [animal] => Dodo bird
    [path] => /usr/local/bin
    [URL] => http://www.example.com/~username
    [phpversion] => Array
        (
            [0] => 5.0
            [1] => 5.1
            [2] => 5.2
            [3] => 5.3
        )

    [urls] => Array
        (
            [svn] => http://svn.php.net
            [git] => http://git.php.net
        )

)
Array
(
    [first_section] => Array
        (
            [one] => 1
            [five] => 5
            [animal] => Dodo bird
        )

    [second_section] => Array
        (
            [path] => /usr/local/bin
            [URL] => http://www.example.com/~username
        )

    [third_section] => Array
        (
            [phpversion] => Array
                (
                    [0] => 5.0
                    [1] => 5.1
                    [2] => 5.2
                    [3] => 5.3
                )

            [urls] => Array
                (
                    [svn] => http://svn.php.net
                    [git] => http://git.php.net
                )

        )

)

Example #3 parse_ini_file() parsing a php.ini file

代码语言:javascript
复制
<?php
// A simple function used for comparing the results below
function yesno($expression)
{
    return($expression ? 'Yes' : 'No');
}

// Get the path to php.ini using the php_ini_loaded_file() 
// function available as of PHP 5.2.4
$ini_path = php_ini_loaded_file();

// Parse php.ini
$ini = parse_ini_file($ini_path);

// Print and compare the values, note that using get_cfg_var()
// will give the same results for parsed and loaded here
echo '(parsed) magic_quotes_gpc = ' . yesno($ini['magic_quotes_gpc']) . PHP_EOL;
echo '(loaded) magic_quotes_gpc = ' . yesno(get_cfg_var('magic_quotes_gpc')) . PHP_EOL;
?>

上面的例子会输出类似于:

代码语言:javascript
复制
(parsed) magic_quotes_gpc = Yes
(loaded) magic_quotes_gpc = Yes

笔记

注意:这个函数与php.ini文件无关。它已在您运行脚本时处理完毕。这个函数可以用来读取你自己的应用程序的配置文件。

注意:如果ini文件中的值包含任何非字母数字字符,它需要用双引号(“)括起来。

注意:有些保留字不能用作ini文件的键。这些包括:null,yes,no,true,false,on,off,none。Value null,off,no和false结果在“,和valuses on,yes和true结果在“1”,除非INI_SCANNER_TYPED使用模式(PHP 5.6.1)。字符?{}|&~!()^"不得在钥匙的任何地方使用,并且在价值中具有特殊的含义。

注意:不带等号的条目将被忽略。例如,“foo”被忽略,而“bar =”被解析并添加一个空值。例如,MySQL在my.cnf中有一个“no-auto-rehash”设置,它没有取值,所以它被忽略。

另请参阅

  • parse_ini_string() - 解析配置字符串

← move_uploaded_file

parse_ini_string →

扫码关注腾讯云开发者

领取腾讯云代金券