Jump to content

Write to specific area of an INI file? HELP!


xZenjix

Recommended Posts

Ok, so I have an INI file...

 

OLD HTML=

NEW HTML=

 

 

I need to write the contents $oldhtml to "OLD HTML=" and the contents of $newhtml to "NEW HTML="

 

To store the contents of an ini file to a variable, I just used

$config = parse_ini_file("config.ini",1);
$email = $config['Email'];
$host = $config['Host'];

 

Cant I just write to the file by selecting ['Old HTML'] and ['NEW HTML'], in a fashion similar to the above code?

Link to comment
Share on other sites

From the Documentation:

 

function write_ini_file($assoc_arr, $path, $has_sections=FALSE) {     

$content = "";     

  if ($has_sections) {         
  foreach ($assoc_arr as $key=>$elem) {             
  
   $content .= "[".$key."]\n";              
   
   foreach ($elem as $key2=>$elem2) {                 
    if(is_array($elem2))                  
    {                      
    for($i=0;$i<count($elem2);$i++)                     
     {                          
     $content .= $key2."[] = \"".$elem2[$i]."\"\n";                      
     }                 
      }                 
      else if($elem2=="") $content .= $key2." = \n";                  
      else $content .= $key2." = \"".$elem2."\"\n";              
      }          
      }      
      }      
      else {          
      foreach ($assoc_arr as $key=>$elem) {             
       if(is_array($elem))             
        {                  
        for($i=0;$i<count($elem);$i++)                  
        {                     
         $content .= $key2."[] = \"".$elem[$i]."\"\n";                 
          }             
           }            
             else if($elem=="") $content .= $key2." = \n";              
             else $content .= $key2." = \"".$elem."\"\n";         
              }     
               }      
                if (!$handle = fopen($path, 'w')) {          
                return false;      
                }     
                 if (!fwrite($handle, $content)) {         
                  return false;      
                  }      
                  fclose($handle);      
                  return true;  
                  } 

Link to comment
Share on other sites

$sample= array(                 
'first' => array(                    
'first1' => 1,                    
'first2' => 2,                     
'first3' => 3,                    
'first4' => 4             
),                
'second' => array(                    
'second1' => 1,                     
'second2' => 2,                    
'second3' => 3,                     
'second4' => 4
)); 
write_ini_file($sample, 'config.ini', true); 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.