How can string replacement functions like str_replace() or strtr() be utilized to dynamically insert database values into email content in PHP?

When sending emails with dynamic content in PHP, you may need to insert database values into the email content. You can achieve this by using string replacement functions like str_replace() or strtr(). These functions allow you to search for placeholders in your email template and replace them with the corresponding database values before sending the email.

// Assume $emailContent is the email template with placeholders for database values
$emailContent = "Hello {username}, your account balance is {balance}.";

// Fetch database values
$username = "John Doe";
$balance = "$1000";

// Replace placeholders with database values
$emailContent = str_replace("{username}", $username, $emailContent);
$emailContent = str_replace("{balance}", $balance, $emailContent);

// Send email with dynamic content
// mail($recipient, $subject, $emailContent);