1

I have the following problem within the following script

PHP code:

$var = file_get_contents('template/default/index.php');
$content1 = str_replace('{php}','<?php',$var);
$content2 = str_replace('{/php}','?>',$content1);
echo $content2; 

template/default/index.php code:

<!DOCTYPE html>
<html>
    <body>
        <h1>My First Heading</h1>
        <p>My first paragraph.</p>
      {php} echo 'worked'; {/php}
    </body>
</html>

I'm trying to transform the {php}{/php} tags within the template/default/index.php to propper php tags. This actually happends, but the php code gets commented out. The browser gives this in return:

<h1>My First Heading</h1>
<p>My first paragraph.</p>
<!--?php echo 'worked'; ?-->

Does anybody know how to solve this?

6
  • are you using wordpress? any framework? Commented Aug 8, 2013 at 13:37
  • check the template/default/index.php content; the comment must be there. Commented Aug 8, 2013 at 13:39
  • 1
    Echoing the PHP like that isn't going to execute it, it would just send the PHP code to the browser, which will just display it. I would guess the Apache or PHP engine is commenting out the PHP code when you echo it. You'll need to either include the file or use the eval function. Commented Aug 8, 2013 at 13:39
  • Echo'ing will print as is, you need to eval it Commented Aug 8, 2013 at 13:42
  • 2
    sounds like happy code injections day =) Commented Aug 8, 2013 at 13:43

2 Answers 2

7

Instead of echoing out the replaced content, it should be eval'd. Although I don't recommend that.

Sign up to request clarification or add additional context in comments.

2 Comments

And how would I do that? Can;t figure it out with eval. Thanks for helping!
or use include, and capture via php output buffers: ob_start(); include($file); $contents = ob_end_clean();
2

I see no reason to do any of this.

Just use <?php and ?> in your index.php file instead of {php} and {/php} and then just include the file in your script.

2 Comments

I think he's trying to eval it like a template file similar to how Smarty template engine does it
Ehhhh I do not like it... @Kieran

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.