PHP file_get_contents然后替换函数中的字符串

时间:2012-11-28 13:11:24

标签: php function file-get-contents phpmailer str-replace

**User_Tpl.html**
<html><head></head>
<body>
Hello {NAME},
Business name : {BUSINESS_NAME}.
Tel : {TELEPHONE}.
Mob : {MOBILE}.
</body>
</html>
function mailFooter($content) {

  $sBusiness = 'Business name';
  $sTelp = '0';
  $sMobile = '0';

  $content = str_replace('{BUSINESS_NAME}', $sBusiness, $content);
  $content = str_replace('{TELEPHONE}', $sTelp, $content);
  $content = str_replace('{MOBILE}', $sMobile, $content);

  return $content;
}

$content = file_get_contents('page/email/User_Tpl.html');
$content = str_replace('{SUBJECT}', $subject, $content);
$content = str_replace('{NAME}', $ownerName, $content);

**mailerFooter($content);**
// always return {BUSINESS_NAME}, {TELEPHONE}

$mail->AddAddress( $email );
$mail->SetFrom($name );
$mail->AddReplyTo( $reply );
$mail->Subject = trim($subject);
$mail->MsgHTML( trim($content) );
$mail->IsHTML(true); 
$mail->Send();

我使用PHPMailer作为Mailer库以及如何在mailFooter()函数中替换那些{strings}。

2 个答案:

答案 0 :(得分:4)

使用数组

$content ='
<html><head></head>
<body>
Hello {NAME},
Business name : {BUSINESS_NAME}.
Tel : {TELEPHONE}.
Mob : {MOBILE}.
</body>
</html>';

$search= array ('{BUSINESS_NAME}','{TELEPHONE}','{MOBILE}');
$replace=array($sBusiness,$sTelp,$sMobile);
$content =str_replace($search,$replace,$content);

答案 1 :(得分:-1)

    private arr = array(
    "{title}" => "Home page",
    "{email}" => "my@mail.com",
    ...
    );
    private $content = "Title for my site - {title}, if you interesting my email addres -> {email}"

$this->content = strtr($this->content, $this->arr);

echo $this->content;

此回报 - &gt; “我网站的标题 - 主页,如果你有意思我的电子邮件地址 - &gt; my@mail.com”