title: Template string replace in php
body: <p>I'm trying to build a system where I can create a file based in a template.
  The template is stored in DB and I populate the template with data sent from a form.</p>
  
  <p>I've tried doing it this way but I can't get to use the array. It always gives me error.</p>
  
  <pre><code>&lt;?php
  
  $string = "test";
  $text = "This is a text for testing";
  
  $rplc_string = '{$string}';
  $rplc_text = '{$text}';
  
  $tpl = '&lt;html&gt;&lt;head&gt;&lt;title&gt;{$string}&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;{$string}&lt;/h1&gt;&lt;p&gt;{$text}&lt;/p&gt;&lt;ul&gt;&lt;?php foreach($array as $key =&gt; $value): ?&gt;&lt;li&gt;&lt;?php echo $key; ?&gt;&lt;/li&gt;&lt;?php endforeach; ?&gt;&lt;/ul&gt;&lt;/body&gt;&lt;/html&gt;';
  
  $tpl = preg_replace($rplc_string, $string, $tpl);
  $tpl = preg_replace($rplc_text, $text, $tpl);
  
  $array = array( 'one' =&gt; '1', 'two' =&gt; '2', 'three' =&gt; '3' );
  
  ob_start();
  eval('?&gt;' . $tpl);
  $output = ob_get_clean();
  
  echo $output;
  
  ?&gt;
  </code></pre>
  
  <p>Is there better way of doing this?</p>