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";
?>
Radek
  • 13,813
  • 52
  • 161
  • 255

2 Answers2

3

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
  • 1,854
  • 2
  • 15
  • 19
  • 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 07 '10 at 04: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 07 '10 at 17:39
  • thank you @Mickey and the last thing would be to search only within particular section :-) – Radek Jul 08 '10 at 00:03
1

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
  • 96,375
  • 17
  • 202
  • 225
  • I read somewhere that `parse_ini_file` ignores comments. I need to save the file as it was. – Radek Jul 07 '10 at 01:00