ntan
ntan

Reputation: 2205

Parse file and interpret php code in it without php tags

I have a text file with the follow content:

This is a static content

{{ echo "Hello World" }}

The file is named test.txt

What I want is to parse this file using PHP and create a new file with the following content:

This is a static content

Hello World

As you can see the code inside the curly braces: {{}} needs to be executed before save to the target file. I don't know where to start to achieve my goal.

Upvotes: 0

Views: 66

Answers (1)

Federkun
Federkun

Reputation: 36924

Something like

echo preg_replace_callback('/\{\{(.*?)\}\}/', function($match) {
    ob_start();
    eval($match[1] . ';');
    $out = ob_get_clean();

    return $out;
}, $string);

should do the job.

Demo: http://3v4l.org/nYT31

Upvotes: 1

Related Questions