Problems with replacing text in a text file
<p>I have the following scenarion.</p>

<p>Everytime my page loads I create a file. Now my file has two tags within. {theme}{/theme} and {layout}{/layout}, now everytime I choose a certain layout or theme it should replace the tags with {layout}layout{/layout} and {theme}theme{/theme}</p>

<p>My issue is that after I run the following code</p>

<pre><code>if(!file_exists($_SESSION['file'])){
        $fh = fopen($_SESSION['file'],"w");
        fwrite($fh,"{theme}{/theme}\n");
        fwrite($fh,"{layout}{/layout}");
        fclose($fh);
    }

    $handle = fopen($_SESSION['file'],'r+');



if ($_REQUEST[theme]) {
        $theme = ($_REQUEST[theme]);
        //Replacing the theme bracket in the cache file for rememberence
        while($line=fgets($handle)){
            $line = preg_replace("/{theme}.*{\/theme}/","{theme}".$theme."{/theme}",$line);
            fwrite($handle, $line);
        }
}
</code></pre>

<p>My output looks as follows </p>

<pre><code>{theme}{/theme}
{theme}green{/theme}
</code></pre>

<p>And it needs to look like this</p>

<pre><code>{theme}green{/theme}
{layout}layout1{/layout}
</code></pre>
