3

我的配置文件如下所示:

title = myTitle;
otherTitle = myOtherTitle;

当我用 file() 读取文件时,它会创建这个数组

[0] => title = myTitle;
[1] => otherTitle = myOtherTitle;

我希望数组看起来像

[title] => myTitle;
[otherTitle] => myOtherTitle;

我是不是用了错误的方法她?我是否应该将整个配置读入一个刺痛并从那里爆炸?

4

2 回答 2

9

您可以使用该parse_ini_file功能。它在 PHP 4 和 5 中可用。

如果您的配置文件如下所示:

one = 1;
five = 5;
animal = BIRD;

该函数将返回以下关联数组:

Array
(
    [one] => 1
    [five] => 5
    [animal] => BIRD
)
于 2009-06-19T22:02:11.810 回答
0

我会遍历文件并单独分解每一行。这是一个简单的示例,之后$config您将按要求保存所有数据。

$config = array();
$lines = file("config.ini");
foreach ($lines as $line) {
    $vals = explode('=', $line, 2);
    $config[trim($vals[0])] = trim($vals[1]);
}
于 2009-06-19T21:58:45.577 回答