阅读、编辑和保存配置文件(php)
我有两个配置文件。一种是ini,一种是php。它们看起来像下面这样。我需要更新数据库文件名,但其余文件必须不变。知道该怎么做吗?
config.ini
; 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";
?>
I have two configuration files. One is ini one is php. They look like below. I need to update the database file name but the rest of the files must be unchanged. Any idea how to do so?
config.ini
; Turning Debugging on
[test]
developer = true
; Production site configuration data
[production]
database.params.dbname = /var/lib/firebird/data/radek_db.gdb
and 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";
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 file_get_contents() 将文件读取为字符串,对其执行 str_replace() 或 preg_replace() 操作,然后使用 file_put_contents() 保存它吗?
我会将这些全部链接到文档,但我没有建立多个链接的声誉...
编辑:如果您只知道选项的名称,则可以使用preg_match 使用正则表达式查找选项名称(类似于
'/^database\.params\.dbname = (.*)$/'
),然后对找到的名称执行 str_replace 。像这样的东西:
Could you read the file to a string with file_get_contents(), do a str_replace() or preg_replace() on it, and then save over it with file_put_contents()?
I'd link those all to the documentation, but I don't have the reputation to make more than one link...
EDIT: If all you know is the name of the option, you can use preg_match to find the option name with a regexp (something like
'/^database\.params\.dbname = (.*)$/'
) and then do a str_replace on the name you find.Something like this:
要读取ini文件,有
parse_ini_file
。对于其余的,您必须为此目的编写一个简单的脚本...或使用sed
。For reading ini files, there's
parse_ini_file
. For the rest, you'll have to write a simple script for that purpose... or usesed
.