title: Execute PHP code in a String without Eval
body: <p>Currently developing a "simple" template class, the problem is how would I execute PHP code within a string without using eval?</p>
  
  <p>A following example is how my template class works:</p>
  
  <pre><code>$user = 'Dave';
  
  ob_start();     
  include 'index.tpl';
  $content = ob_get_clean(); // String
  
  $pattern = sprintf('/%s\s*(.+?)\s*%s/s', '{{', '}}'); // replace with php tags
  $new_content = preg_replace($pattern, '&lt;?php echo $1; ?&gt;', $content);
  
  echo $new_content; 
  </code></pre>
  
  <p>index.tpl</p>
  
  <pre><code>&lt;html&gt;
       &lt;head&gt;&lt;/head&gt;
       &lt;body&gt;
           Hello {{ $user }}!
       &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I get the following result:</p>
  
  <pre><code>Hello !
  </code></pre>
  
  <p>I don't want to use eval because how slow and bad it is to use, is there any other way of doing this? laravel blade engine does not use eval so there must be.</p>
  
  <p>Thanks,</p>
  
  <p>Joel.</p>