Have you heard of strtr()?
It serves this very purpose and is very useful to create dynamic HTML content containing information from a database, for example.
Given the following string:
$str = 'here is some text to greet user {zUserName}';
then you can parse it using strtr()
:
$userName = 'Mike';
$parsed = strtr($str,array('{zUserName}'=>$userName));
echo $parsed; // outputs: 'here is some text to greet user Mike'
While sprintf
is faster in some regards, strtr
allows you to control what goes where in a more friendly way (sprintf
is not really manageable on very long strings containing, say, a hundred placeholders to be replaced).