美元($)在电子邮件模板中无效

jog*_*_pi 1 php email email-client html-email

我已经构建了自定义电子邮件模板.并指定了一些变量{#paid_amount}等等.

所有变量都被替换但paid_amount不是预期的.我已经取代了这样的东西:

// 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);
Run Code Online (Sandbox Code Playgroud)

它.00在电子邮件中打印金额,如果我$从打印金额中删除标志1.00.我在Gmail中测试了它.以前有人遇到过这个吗?

即使我尝试$但不工作.任何人都可以告诉我我错过了什么或为什么它不工作?

hek*_*mgl 7

你需要逃脱美元符号:

$paid_amount = '\$1.00';
Run Code Online (Sandbox Code Playgroud)

这是因为,preg_replace()使用$在替换参数,以解决捕获组的内容.

例:

$string = ">> hello <<";
$pattern = "/>> ([^ ]*) <</";

echo preg_replace($pattern, '$1', $string);
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,$1解决了第一个捕获组的内容:([^ ]*)- >"hello".