【问题标题】:preg_replace on email template电子邮件模板上的 preg_replace
【发布时间】:2015-03-19 03:49:58
【问题描述】:

我使用 swift mailer ,当我从服务器收到一封电子邮件时,我得到了这个:

您通过忘记密码请求您的密码

您的用户名是:{myusername}

您的密码是:{mypassword}

使用 { } ,发送的功能是:

function format_emailforget($info, $format){

    //set the root
    $root = $_SERVER['DOCUMENT_ROOT'].'/email';

    //grab the template content
    $template = file_get_contents($root.'/forget_template.'.$format);

    //replace all the tags
    $template = preg_replace('{USERNAME}', $info['username'], $template);
    $template = preg_replace('{EMAIL}', $info['email'], $template);
    $template = preg_replace('{PASSWORD}', $info['password'], $template);
    $template = preg_replace('{SITEPATH}','http://smracer.com', $template);

    //return the html of the template
    return $template;

}

这段代码哪里不好?

【问题讨论】:

  • 一方面,您没有使用preg_replace 对。模式需要分隔符,{ / } 是特殊字符。请参阅文档以获取示例,或者,由于您的模板语言非常简单,因此只需使用 str_replace 代替。
  • 谢谢 ceejayoz ,我替换了它,并且没有 {}

标签: php email swiftmailer


【解决方案1】:

更优雅的方式和易于维护:)

$pattern     = array('/\{USERNAME\}/' , '/\{EMAIL\}/' , '/\{PASSWORD\}/' , '/\{SITEPATH\}/');
$replacement = array($info['username'], $info['email'], $info['password'], 'http://smracer.com');

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-31
    • 2013-08-12
    • 2021-09-12
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    相关资源
    最近更新 更多