<?php
function template($template, $vals)
{
$matches = array();
$regex = "/\{\{(\S+)\}\}/e";
$rendered = $template; // in case no matches are found
preg_match_all($regex, $template, $matches, PREG_OFFSET_CAPTURE, 3);
foreach ($matches[0] as $found) {
$block = $found[0];
$offset = $found[0];
$k = str_replace('{{', '', $block);
$k = str_replace('}-}', '', $k);
$rendered = preg_replace($regex, '$vals[\\1]', $template);
}
return $rendered;
}
$template = "test {{ME}}.";
$vals = array('ME' => 'beep');
echo template($template, $vals);
preferences:
46.34 ms | 402 KiB | 5 Q