What are the potential pitfalls of using str_replace for variable replacement in PHP templates?

Using str_replace for variable replacement in PHP templates can lead to unintended replacements if the search strings are not unique enough. It can also be less efficient than using a templating engine like Twig, which is specifically designed for this purpose. To avoid these pitfalls, consider using a templating engine or implementing a more robust variable replacement method.

// Example of using str_replace for variable replacement in PHP templates
$template = "Hello, {name}! Today is {day}.";
$replacements = array(
    '{name}' => 'John',
    '{day}' => 'Monday'
);

// Potential pitfall: using str_replace for variable replacement
$html = str_replace(array_keys($replacements), array_values($replacements), $template);

// Better approach: using a templating engine like Twig
// Example using Twig
// $loader = new Twig_Loader_Filesystem('/path/to/templates');
// $twig = new Twig_Environment($loader);
// $html = $twig->render('template.html', $replacements);