views:

46

answers:

2

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";
?>
A: 

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.

Artefacto
I read somewhere that `parse_ini_file` ignores comments. I need to save the file as it was.
Radek
@Rad Then you have no chance but to do it manually.
Artefacto
By manually I mean of course writing a script :p
Artefacto
+1  A: 

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);
Michael Louis Thaler
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
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
thank you @Mickey and the last thing would be to search only within particular section :-)
Radek