- preg_replace_callback: documentation ( source)
<?php
function stringInterpolate($str, array $vars)
{
return preg_replace_callback('/\{\$(\w+)\}/', function($matches){
return $matches[0];
}, $str);
}
echo stringInterpolate('{$name} {$surname}', array('name' => 'foo', 'surname' => 'Bar'));