[solved]Using php_parse_ini and saving afterwards

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

[solved]Using php_parse_ini and saving afterwards

Post by jmut »

Hi,
I want to create this configurable file. But the configuration will be made from external systems.
So except for parsing the content for using it.....I should also be able to modify it.

I see parse_ini_file parses the file pretty good...and I can easily use such structure of the file.

My question is is there any easy way to modify the data and save the contents back to the file keeping this structure.
Thanks in advance
Last edited by jmut on Wed Nov 16, 2005 9:18 am, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I think a websearch for "php ini writer" will return you a couple of classes/functions that can do exactly what you want.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

look no further than the comments by tomasz.frelik(at)enzo.pl for parse_ini_file()

Code: Select all

function write_ini_file($path, $assoc_array) {

   foreach ($assoc_array as $key => $item) {
       if (is_array($item)) {
           $content .= "\n[$key]\n";
           foreach ($item as $key2 => $item2) {
               $content .= "$key2 = \"$item2\"\n";
           }       
       } else {
           $content .= "$key = \"$item\"\n";
       }
   }       
  
   if (!$handle = fopen($path, 'w')) {
       return false;
   }
   if (!fwrite($handle, $content)) {
       return false;
   }
   fclose($handle);
   return true;
}
Post Reply