2012-04-11 198 views
2

我有一個函數發出站點電子郵件(使用phpmailer),我想要做的事情基本上是用php來替換email.tpl文件中的所有placheholder與我提供的內容。對我來說,問題是我不想重複代碼,因此我創建了一個函數(見下文)。用PHP替換多個佔位符?

沒有一個PHP函數,我會做一個腳本

// email template file 
$email_template = "email.tpl"; 

// Get contact form template from file 
$message = file_get_contents($email_template); 

// Replace place holders in email template 
$message = str_replace("[{USERNAME}]", $username, $message); 
$message = str_replace("[{EMAIL}]", $email, $message); 

現在我知道該怎麼做休息下,但我粘在str_replace(),如上圖所示,我有多個str_replace()功能,以取代電子郵件模板中的佔位符。我想是的str_replace()添加到我的功能(如下圖),並把它找到[\]所有實例電子郵件模板,我給它,並用佔位符值替換它,我會給這樣的:str_replace("[\]", 'replace_with', $email_body)

問題是我不知道如何將多個佔位符和它們的替換值傳入我的函數,並獲得str_replace("[{\}]", 'replace_with', $email_body)來處理我給它的所有佔位符,並用相應的值替換。

因爲我想在多個地方使用該功能並避免重複代碼,所以在某些腳本上,我可能會傳遞函數5個佔位符,並且值和另一個腳本可能需要傳遞10個佔位符,並將值傳遞給要使用的函數在電子郵件模板。

我不確定是否需要在腳本中使用一個數組,該函數將使用該函數並在函數中使用for循環,以便讓我的php函數獲得xx佔位符和xx值從一個腳本,並通過佔位符循環,並取代它們的值。

這是我上面提到的功能。我評論了這個腳本,這可能更容易解釋。

// WILL NEED TO PASS PERHAPS AN ARRAY OF MY PLACEHOLDERS AND THERE VALUES FROM x SCRIPT 
// INTO THE FUNCTION ? 
function phpmailer($to_email, $email_subject, $email_body, $email_tpl) { 

// include php mailer class 
require_once("class.phpmailer.php"); 

// send to email (receipent) 
global $to_email; 
// add the body for mail 
global $email_subject; 
// email message body 
global $email_body; 
// email template 
global $email_tpl; 

// get email template 
$message = file_get_contents($email_tpl); 

// replace email template placeholders with content from x script 
// FIND ALL INSTANCES OF [{}] IN EMAIL TEMPLATE THAT I FEED THE FUNCTION 
// WITH AND REPLACE IT WITH THERE CORRESPOING VALUES. 
// NOT SURE IF I NEED A FOR LOOP HERE PERHAPS TO LOOP THROUGH ALL 
// PLACEHOLDERS I FEED THE FUNCTION WITH AND REPLACE WITH THERE CORRESPONDING VALUES 
$email_body  = str_replace("[{\}]", 'replace', $email_body); 

// create object of PHPMailer 
$mail = new PHPMailer(); 

// inform class to use smtp 
$mail->IsSMTP(); 
// enable smtp authentication 
$mail->SMTPAuth = SMTP_AUTH; 
// host of the smtp server 
$mail->Host  = SMTP_HOST; 
// port of the smtp server 
$mail->Port  = SMTP_PORT; 
// smtp user name 
$mail->Username = SMTP_USER; 
// smtp user password 
$mail->Password = SMTP_PASS; 
// mail charset 
$mail->CharSet = MAIL_CHARSET; 

// set from email address 
$mail->SetFrom(FROM_EMAIL); 
// to address 
$mail->AddAddress($to_email); 
// email subject 
$mail->Subject = $email_subject; 
// html message body 
$mail->MsgHTML($email_body); 
// plain text message body (no html) 
$mail->AltBody(strip_tags($email_body)); 

// finally send the mail 
if(!$mail->Send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
    } else { 
    echo "Message sent Successfully!"; 
    } 
} 

回答

13

簡單,看strtr­Docs

$vars = array(
    "[{USERNAME}]" => $username, 
    "[{EMAIL}]" => $email, 
); 

$message = strtr($message, $vars); 

添加儘可能多(或更少)的更換,對,只要你喜歡。但我建議,你處理模板調用phpmailer函數之前,這樣的事情是保持分開:模板和電子郵件發送:

class MessageTemplateFile 
{ 
    /** 
    * @var string 
    */ 
    private $file; 
    /** 
    * @var string[] varname => string value 
    */ 
    private $vars; 

    public function __construct($file, array $vars = array()) 
    { 
     $this->file = (string)$file; 
     $this->setVars($vars); 
    } 

    public function setVars(array $vars) 
    { 
     $this->vars = $vars; 
    } 

    public function getTemplateText() 
    { 
     return file_get_contents($this->file); 
    } 

    public function __toString() 
    { 
     return strtr($this->getTemplateText(), $this->getReplacementPairs()); 
    } 

    private function getReplacementPairs() 
    { 
     $pairs = array(); 
     foreach ($this->vars as $name => $value) 
     { 
      $key = sprintf('[{%s}]', strtoupper($name)); 
      $pairs[$key] = (string)$value; 
     } 
     return $pairs; 
    } 
} 

的使用可大大簡化的話,你可以將整個模板傳遞給任何函數需要字符串輸入。

$vars = compact('username', 'message'); 
$message = new MessageTemplateFile('email.tpl', $vars); 
+0

非常感謝你,第一位代碼向我解釋了這一切,你所做的這個類非常感謝,但是我還沒有學會oop,所以會給它一個錯誤,但是第一個樣例很適合我需要的謝謝,非常感謝! :) – PHPLOVER 2012-04-11 13:35:37

+1

@PHPLover:那麼請參閱以及[類似的電子郵件模板問題和答案](http://stackoverflow.com/q/8176804/367456)和[用變量替換佔位符的有效方法](http:/ /stackoverflow.com/q/7980741/367456)。 – hakre 2012-04-11 13:56:12

0

爲什麼不把電子郵件模板做成php文件呢?然後你可以這樣做:

Hello <?=$name?>, my name is <?=$your_name?>, today is <?=$date?> 

裏面的電子郵件生成您的HTML,然後發送結果作爲電子郵件。

對我來說,似乎你喜歡你這麼辛苦嗎?

+0

因爲它可能會引入代碼注入漏洞?順便說一句短標籤已棄用 – symcbean 2012-04-11 12:53:42

+0

如果你用$ y替換「X」,那麼它的結果與用$ y替換$ x的結果是一樣的 - 所以沒有新的代碼注入漏洞? – Laurence 2012-04-11 13:57:09

+0

你是在說服模板的控制方式與php代碼 – symcbean 2012-04-11 16:27:12