I have a PHP script with some variables set in the global namespace:
$domain = 'example.com';
$hostname = 'www.' . $domain;
I am reading an external file into a string variable in my script using file_get_contents:
$file_contents = file_get_contents('external_file.tpl');
The external file (and the string $file_contents
) contains placeholders which correspond to variable names.
127.0.0.1 {{domain}} {{hostname}}
I would like to have all the placeholders in the variable $file_contents
replaced with their respective PHP variables already set. I want this to work generically (without hard-coding placeholder / variable names). I do not want to edit the file, just the contents of the string.
What's the easiest way to do this?