Skip to content

Instantly share code, notes, and snippets.

@danielrw7
Last active July 22, 2016 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielrw7/f98d191f2c7938845d48804160f57999 to your computer and use it in GitHub Desktop.
Save danielrw7/f98d191f2c7938845d48804160f57999 to your computer and use it in GitHub Desktop.
<?php
function replace_template_strings($template, $data = array(), $beginning_regex = '\{\{', $end_regex = '\}\}') {
return preg_replace_callback('/' . $beginning_regex . '(\w+)' . $end_regex . '/', function($match) use ($data) {
return empty($data[$match[1]]) ? "" : $data[$match[1]];
}, $template);
}
<?php
require("replace_template_strings.php");
$template = <<<TEMPLATE
Hello {{firstname}}, {{lastname}}!
TEMPLATE;
$data = array(
"firstname" => "John",
"lastname" => "Smith"
);
echo replace_template_strings($template, $data) . "\n";
// => Hello John, Smith!
@BrandonDyer64
Copy link

This is very

@weaversam8
Copy link

very useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment