2

我有两个配置文件。一个是ini一个是php。它们如下所示。我需要更新数据库文件名,但其余文件必须保持不变。知道怎么做吗?

配置文件

; Turning Debugging on
[test]
developer = true

; Production site configuration data
[production]
database.params.dbname   = /var/lib/firebird/data/radek_db.gdb

和setting.php

<?php
/**
The settings file
*/

#This will be done automatically if u want to override it uncomment the next line few lines
/*
    $Path   = 'mainline';

#Database to connect to:
    $Database        =       "radek_db";
?>
4

2 回答 2

3

您可以使用 file_get_contents() 将文件读取为字符串,对其执行 str_replace() 或 preg_replace(),然后使用 file_put_contents() 保存它吗?

我会将所有这些链接到文档,但我没有建立多个链接的声誉......

编辑:如果您只知道选项的名称,则可以使用preg_match使用正则表达式(类似于'/^database\.params\.dbname = (.*)$/')查找选项名称,然后对找到的名称执行 str_replace 。

像这样的东西:

$file = file_get_contents('/path/to/config/file');
$matches = array();
preg_match('/^database\.params\.dbname = (.*)$/', $file, $matches);
$file = str_replace($matches[1], $new_value, $file);
file_put_contents('/path/to/config/file', $file);
于 2010-07-07T03:17:20.217 回答
1

对于读取 ini 文件,有parse_ini_file. 其余的,您必须为此目的编写一个简单的脚本......或使用sed.

于 2010-07-07T00:38:17.960 回答