Take the 2-minute tour ×

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?


; Turning Debugging on
developer = true

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

and setting.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";
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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);
share|improve this answer
more than string replace I need to find the option and then replace it's value. Not sure if it could be done this way –  Radek Jul 7 '10 at 4:03
Ahhh--I was assuming you knew what the current setting on the option to change was. I am amending my answer appropriately. –  Michael Louis Thaler Jul 7 '10 at 17:39
thank you @Mickey and the last thing would be to search only within particular section :-) –  Radek Jul 8 '10 at 0:03
add comment

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.

share|improve this answer
I read somewhere that parse_ini_file ignores comments. I need to save the file as it was. –  Radek Jul 7 '10 at 1:00
@Rad Then you have no chance but to do it manually. –  Artefacto Jul 7 '10 at 2:03
By manually I mean of course writing a script :p –  Artefacto Jul 7 '10 at 12:12
add comment

Your Answer


