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?

Aayushi Jain
  • 2,861
  • 2
  • 29
  • 36

2 Answers2

7

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

pritaeas
  • 2,073
  • 5
  • 34
  • 51
  • And how would I do that? Can;t figure it out with eval. Thanks for helping! –  Aug 08 '13 at 13:44
  • 3
    or use include, and capture via php output buffers: `ob_start(); include($file); $contents = ob_end_clean();` – Daniel W. Aug 08 '13 at 13:49
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.

Naftali
  • 144,921
  • 39
  • 244
  • 303