 <pre><code>preg_match_all('/\[(.*?)\]/', $string, $out); foreach ($out[1] as $k =&gt; $v) { eval("\$result = $v;"); $string = str_replace($out[0][$k], $result, $string); } </code></pre> <p>This code is <em>highly</em> dangerous if the strings are user inputs because it allows <em>any</em> arbitrary code to be executed</p>