Is it possible to parse an INI file with comments and edit it?
<p>I have created a form,I read from a INI file and show its values in the form.
Now I want to edit that file.</p>

<p>So I show values in text box and let the user edit the file.</p>

<p>Now I am using INI Library to write my files, but my code changes the structure of INI file, also it remove the comments.</p>

<p>Is it possible that when I write to the file only those value is changed and other things like comments and Section remain as it is ?</p>

<p>This is my code:</p>

<pre><code>$CFG_file = $this-&gt;data['file_data'] = parse_ini_file("CFG/fms.cfg" ,true);


        if (isset($_POST['submit'])) 
        {   

            //var_dump($_POST);
            $data_file = $this-&gt;parameter_m-&gt;array_from_post(array('VAL1','VAL2','InMedia','VAL','VAL','VAL5','VAL3'));

            $this-&gt;load-&gt;helper('file');
            $file = "CFG/fms.cfg";
            $this-&gt;load-&gt;library('ini');
            $ini = new INI($file);
            $ini-&gt;write($file, $data_file );    
        }
</code></pre>

<p>And the library code that writes file :</p>

<pre><code>function write($file = NULL, $file_content = array(), $sections = FALSE) {
    $this-&gt;file_content = (!empty($file_content)) ? $file_content : $this-&gt;file_content;
    $this-&gt;file = ($file) ? $file : $this-&gt;file;
    $this-&gt;sections = $sections;
    $content = NULL;

    if ($this-&gt;sections) {
        foreach ($this-&gt;file_content as $section =&gt; $file_content) {
            $content .= '[' . $section . ']' . PHP_EOL;
            foreach ($file_content as $key =&gt; $val) {
                if (is_array($val)) {
                    foreach ($val as $v) {
                        $content .= $key . '[]=' . (is_numeric($v) ? $v : $v ) . PHP_EOL;
                    }
                } elseif (empty($val)) {
                    $content .= $key . '=' . PHP_EOL;
                } else {
                    $content .= $key . '=' . (is_numeric($val) ? $val : $val ) . PHP_EOL;
                }
            }
            $content .= PHP_EOL;
        }
    } else {
        foreach ($this-&gt;file_content as $key =&gt; $val) {
            if (is_array($val)) {
                foreach ($val as $v) {
                    $content .= $key . '[] = ' . (is_numeric($v) ? $v : '"' . $v . '"') . PHP_EOL;
                }
            } elseif (empty($val)) {
                $content .= $key . ' = ' . PHP_EOL;
            } else {
                $content .= $key . ' = ' . (is_numeric($val) ? $val : '"' . $val . '"') . PHP_EOL;
            }
        }
    }

    return (($handle = fopen($this-&gt;file, 'w+')) &amp;&amp; fwrite($handle, trim($content)) &amp;&amp; fclose($handle)) ? TRUE : FALSE;
}
</code></pre>

<p>And this is my file :</p>

<pre><code>    []
VAL1= 0.0.0.0
VAL2= 5070

;media settings

;ON/OFF/AUTO
VAL3= OFF

VAL4= 8000 
VAL5= 4000
VAL6= 360

;Transport settings

;ON/OFF
VAL7= OFF
</code></pre>

<p>When i execute my code this becomes  :</p>

<pre><code>VAL1= "0.0.0.0"
VAL2 = 5071
VAL3 = 
VAL4 = 8000
VAL5 = 4000
VAL6 = 360
VAL7 = "ON"
</code></pre>