Reputation: 33
I have made a template engine, which uses file_get_contents()
to get the contents of a page then replace any keys it finds with the values.
That is not an issue, but what is an issue is that it makes a new output for the page, so it is something like this
$output = str_replace($key, $replace, $output); return $output;
The only problem is, in the new output, PHP code is shown in the source code and not ran. Is there any way to fix this?
Upvotes: 0
Views: 79
Reputation: 1198
you can use PHP eval function to run a string as php code
$output = str_replace($key, $replace, $output); return $output;
eval($output);
Upvotes: 2
Reputation: 4798
Once you get your code using file_get_contents()
$code = file_get_contents("page.php");
you do the work you need to do on the variable $code
which is a string then you do this
eval($code);
if the code starts with html code and it has some php code inside , try this
eval(" ?> " . $code . " <?php ");
I have to tell you that this is not a good solution, eval is the most risky function to use on php.
Upvotes: 2