2015-01-20 108 views
-1

我使用快捷邮件,当我从服务器收到一封电子邮件,我得到这个:的preg_replace在电子邮件模板

你要求你的密码通过忘记密码

您的用户名是:{名为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; 

} 

这个代码糟糕的地方在哪里?

+1

其一,你没有使用'preg_replace'权。该模式需要分隔符,'{'/'}'是特殊字符。请参考文档中的示例,或者,因为您的模板语言非常简单,只需使用'str_replace'。 – ceejayoz 2015-01-20 19:18:52

+0

谢谢ceejayoz,我取代了,我没有{}顺利接收,顺便说一句@mario我把myusername,因为我认为这是不相关的,把我的用户名:) – d0z3 2015-01-20 19:30:31

回答

0

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

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

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