Search code examples
phpemailhtml-emailemail-client

Dollar($) sign not working in Email template


I have built a custom email template. And assigned some variables with {#paid_amount} and so on.

All the variables get replaced but paid_amount not as expected. I have replaced something like this:

// Text file with HTML markups
$template = file_get_contents($template_url);

$paid_amount = '$1.00';
$pattern = array( 
              '/\{\#user_name\}/i', 
              '/\{\#paid_amount\}/i', 
              '/\{\#duration\}/i'  );
$replacement = array( 
              $user_name, 
              $paid_amount, 
              $duration );

$new_template = preg_replace($pattern, $replacement, $template);

Its print the amount .00 in the email, and if i remove the sign $ from the amount it print the 1.00. I tested it in Gmail. Has anyone faced this before?

Even i tried with $ but not working. Can anyone please tell me what i have missed or why it is not working?


Solution

  • You need to escape the dollar sign:

    $paid_amount = '\$1.00';
    

    This is because preg_replace() is using the $ in the replace parameter to address the contents of a capturing group.

    Example:

    $string = ">> hello <<";
    $pattern = "/>> ([^ ]*) <</";
    
    echo preg_replace($pattern, '$1', $string);
    

    In the above example, $1 addresses the contents of the first capturing group: ([^ ]*) -> "hello".