开发者

read, edit & save config files (php)

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 开发者_Go百科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";
?>


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:

$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);


For reading ini files, there's parse_ini_file. For the rest, you'll have to write a simple script for that purpose... or use sed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜