More elegant way to parse inline variables in strings
- by Tom
Currently I have this:
function parse_string($string, $variables){
extract($variables);
return eval('return "'. addcslashes($string, '"') .'";');
}
So I can input this string:
'Hi {$name}, my name is {$own_name}'
Together with this array:
array('name' => 'John', 'own_name' => 'Tom')
And get this back:
'Hi John, my name is Tom'
I've never liked this eval() approach but it works and it's fast (faster than regex at least).
Question: Is there a more elegant way to do this (faster than using regex) in PHP5?