【问题标题】:Dollar($) sign not working in Email template美元($)符号在电子邮件模板中不起作用
【发布时间】:2014-06-13 11:46:35
【问题描述】:

我已经建立了一个自定义电子邮件模板。并用{#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);

它在电子邮件中打印.00 的金额,如果我从金额中删除$ 的符号,它会打印1.00。我在 Gmail 中对其进行了测试。有没有人遇到过这个问题?

即使我尝试使用$,但还是不行。谁能告诉我我错过了什么或为什么它不起作用?

【问题讨论】:

    标签: php email html-email email-client


    【解决方案1】:

    你需要转义美元符号:

    $paid_amount = '\$1.00';
    

    这是因为preg_replace()replace 参数中使用$ 来处理捕获组的内容。

    例子:

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

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

    【讨论】:

    • 很难用(我的)语言来解释,但举个例子就很容易...... :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    • 1970-01-01
    相关资源
    最近更新 更多