Str_replace alone

If we know beforehand what the curly braces may contain, we can use do the replacing the other way around: instead of searching for the pattern and replacing the value, we simply replace all values we can think of. With this method, we loop through our array of possible values, generate the possible pattern for it and replace that:

$data = array('planet' => 'World', 'name' => 'Wiebe');
$template = 'Hello {planet}. My name is {name}.';

foreach ($data as $key => $value)
{
        $pattern = '{' . $key . '}';
        $replacements[$pattern] = $value;
}

echo str_replace(array_keys($replacements), array_values($replacements), $template);

With this method, we do not use regular expressions, but only simple replacements. It may be that we try to replace a placeholder in our template which isn't there, but that is not big deal.

This method is not always available. Sometimes you have very many possible placeholder values or it may be expensive to get the values for placeholders. However, if it is possible, this method is by far the simplest.