preg_replace 在电子邮件模板上

preg_replace on email template

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

您通过忘记密码请求密码

您的用户名是:{myusername}

您的密码是:{mypassword}

with { } , 发送函数是:

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;

}

这段代码哪里不好?

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

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

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