Execute PHP code in a string

eval, php

Solution

Needless to say you should find another solution ASAP. In the meantime you can eval the code like this:

$str = '<h1>Welcome</h1><?php echo $motto?><br/>'; // Your DB content

eval("?> $str <?php ");

Demo: http://codepad.org/ao2PPHN7

I can't stress that enough: eval is dangerous, and application code shouldn't be in the database. Try a template parser like Smarty, Dwoo, or my favorite: Twig.

Problem

I have my page contents saved in a database and would like to execute any php code in the string. So if my string was: ``` <h1>Welcome</h1><?php echo $motto?><br/> ``` I only want to execute `echo $motto`. Using eval() will try to execute `<h1>Welcome</h1>`. Any way to do this?

Original source