0

Sorry, I am confused to create a correct title. This is my problem, I have a TXT file:

{{title}}<br />{{content}}

And this is the PHP file: I load file.txt and replace tags {{variable}} with value from $data['variable']

$data = array ( 'title' => 'Hello', 'content' => 'WORLD!!');
$file = file_get_contents("file.txt");
$key = array_keys($data);
$value = array_values($data);
$file = str_replace($key, $value, $file);
echo $file;

nothing change from file.txt

I have 2 way to solve this by assign array's key in this format

'{{variable}}' => 'value' 

or write

str_replace(array('{{','}}'),'',$file) 

before

echo $file; 

Is there are another way?

thx before...

2 Answers 2

1

Yes.

function bracelize($str) {
   return '{' . $str . '}';
}

$search = array_map('bracelize', $key);

Then just use the $search array.

If you are using PHP >= 5.3 you can use an anonymous function if you don't want to pollute the namespace:

$search = array_map(function($str){ return '{'. $str .'}';}, $key);

That being said, if you are planning to use this as an HTML templating system, please don't. PHP itself is a templating engine; why add more overhead? Read this question.

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

5 Comments

PHP is a templating engine, but if you don't trust the template writer, it may very well be necessary to define a simpler template language inside PHP.
And PHP for templateing isn't really nice to write nor to look at.
@nick Well, I think it's just fine, but to each his own.
well, I am thinking just like @NullUserException, php itself is templating engine and I am developing my own simple cms or framework for my team. I didnt use any php templating like smarty instead php tags inside html page. But I'm also thinking to create a quick replacement system in the html using own tag like that.
@Vina You should look really into MVC and write Views in pure PHP. There is also a number of open source MVC frameworks available: CakePHP, CodeIgniter, Symfony just to name a few.
0
$data = array ( 'title' => 'Hello', 'content' => 'WORLD!!');
$file = file_get_contents("file.txt");
foreach ($data as $key => $value) {
    $file = str_replace('{{' . $key . '}}', $value);
}
echo $file;

though probably this will be slower.

Another version using regex:

$file = preg_replace_callback('#\{\{([A-Za-z0-9]+)\}\}#', function($match) use($data) {
    if (isset($data[$match[1]])) {
        return $data[$match[1]];
    }
}, $file);

Another (and my preferred version) is to replace all #{{([A-Za-z0-9)}}# in the file with <?php echo isset($data['$1']) ? $data['$1'] : null; ?>, cache the resulting PHP Code and then execute providing the $data array ;)

1 Comment

Note that the version using regex needs PHP >= 5.3

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.