I have a string, that can has simple templates. And I have an array with values for replacemenet. Currently I am doing it with loop. But I want to change it to preg_replace. Can
Something like the following?
120,
'name' => 'Jim'
);
$string = 'Hello . Your ID is ';
function foo($val) {
return '//';
}
echo preg_replace(array_map('foo', array_keys($values)), array_values($values), $string);
If the whole thing is in a class:
class Template {
static function bar($val) {
return '//';
}
function render($values, $string) {
echo preg_replace(array_map(array('Template', 'bar'), array_keys($values)), array_values($values), $string);
}
}
$values = array(
'id' => 120,
'name' => 'Jim'
);
$string = 'Hello . Your ID is ';
$T = new Template();
$T->render($values, $string);