What are the advantages and disadvantages of using preg_replace in PHP template systems?

When using preg_replace in PHP template systems, the advantage is that it allows for powerful pattern matching and replacement within the template content. This can be useful for dynamically modifying the template output based on specific criteria. However, the disadvantage is that using complex regular expressions can make the code harder to read and maintain, and may also impact performance.

// Example of using preg_replace in a PHP template system
$template = "<p>Hello, {name}!</p>";
$data = array("name" => "John");

// Using preg_replace to replace placeholders with actual values
foreach ($data as $key => $value) {
    $template = preg_replace("/{" . $key . "}/", $value, $template);
}

echo $template;