php replace $variable in string with the content of $variable
<p>In PHP I have the need to generate HTML pages from a template and insert variables in specific places then save them to disk.</p>

<p>So far this code works well:</p>

<pre><code>&lt;?php 

$client = "John doe";

$template = "

&lt;html&gt;
&lt;body&gt;
&lt;div&gt;
$client
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

";

$htmlCode = $template;

$fh = fopen("page.html", 'w') or die("can't open file");
fwrite($fh, $htmlCode);
fclose($fh);

?&gt;
</code></pre>

<p>ultimately I want to slurp and read in the $template from a file on disk, using something like: </p>

<p><code>$template = file_get_contents("template.html");</code></p>

<p>But using this approach I can not get the $client replaced with the "John doe" instead it shows as $client in the HTML code when I save it to disk. </p>

<p>I did read some posts about using regex and printf to replace substings, but i could not see how can I use them in my scenario. I am positive there must be easier and better way.</p>

<p>There will be a need of a lot of variables plopped in many places so I do not want to create a regex for each one. I just want to be able to stick a $variable anywhere in the external html file and PHP should replace it with a variable content (if it exist). Unfortunately file_get_contents() only slurps it into a string exactly as it is without any interpretations. I was wandering if there is another function that I can use for slurping that will actually work as I indented. </p>

<p>Any suggestions are greatly appreciated. </p>
