【问题标题】:More than one instance of str_replace breaks php code不止一个 str_replace 实例破坏了 php 代码
【发布时间】:2012-02-09 01:25:18
【问题描述】:

我正在使用 phpmailer,并根据我的需要对其进行了定制。 $templatemessage 内部是给客户的消息,字段为 {name} 和 {fuel}。

这两个字段被 str_replace 函数替换。

在这个问题出现之前,我只是用 str_replace 替换了 {name},这很好用,将人名导入到那个地方。 当我添加额外的 str_replace 函数以将 {fuel} 替换为 $templatemessage 值时,问题就出现了。现在发生的是 {fuel} 值被导入,但它似乎对 {name} 函数做了一些事情,因为它不再导入名称,而是在电子邮件中只显示 {name},就像我之前添加 {燃料} 它奏效了。 怎样才能让两人好好相处呢?

                  <?php
                  $formid = mysql_real_escape_string($_GET[token]);
                              $templatequery = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addmailinglistmessage WHERE cf_id = '$formid'") or die(mysql_error());
                              $templateData = mysql_fetch_object($templatequery);

                              $gasoiluserTemplate = $templateData->gasoilusers;
                              $dervuserTemplate = $templateData->dervusers;
                              $kerouserTemplate = $templateData->kerousers;
                              $templateMessage = $templateData->mailinglistgroupmessage;
                              $templatename = $templateData->mailinglistgroupname;


                require_once('./send/class.phpmailer.php');

               $mailer= new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch

               // Grab the FreakMailer class
                require_once('./send/MailClass.inc');

                // Grab our config settings
                require_once('./send/config.php');

              // Setup body
              $htmlBody = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                              <html xmlns="http://www.w3.org/1999/xhtml">
                              <head>
                              <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                              <style>#title {text-align:center;font-family:"Times New Roman", Times, serif; font-size:130% !important; font-weight:bold; color:#fff;} .address {color:#fff; font-size:60%; font-family:Verdana, Geneva, sans-serif;}</style>
                              </head>

                              <body>
                              <div style="background:
                                                                none repeat scroll 0% 0% rgb(6, 38,
                                                                97); width:650px; height:auto;">
                              <img id="_x0000_i1030" style="padding-left:5%;padding-right:5%"
                                                                    src="http://www.chandlersoil.com/images/newsletter/header.gif"
                                                                    alt="Chandlers Oil and Gas"
                                                                    border="0" height="112"
                                                                    width="580">
                                                                    <div id="title">' . $templateMessage . '</div>
                                                                    <div style="background:#344ea2; width:501px; height:65px; margin-left:70px;"></div>

                                                                    <div style="background:#13155C; height:30px; width:501px; margin-left:70px;"></div>

                                                                    <div style="background:#fff; width:501px; height:365px; margin-left:70px;"></div>

<div style="background:
                                                                none repeat scroll 0% 0% rgb(6, 38,
                                                                97); height:60px; width:501px; margin-left:70px;"></div>    

                                                                <div style="background:#000139;height:100px; width:580px; margin-left:35px;" >
                                                                  <table cellspacing="0" cellpadding="0" class="address" style="margin-left:5px;">
                                                                    <tr>
                                                                      <td valign="top" width="21%"><p><strong>Chandlers                                                    Depots</strong></p>
                                                                        <table class="address" border="0" cellpadding="0" cellspacing="0" width="87%">
                                                                          <tbody>
                                                                            <tr>
                                                                              <td width="57%"><p>Grantham</p></td>
                                                                              <td width="43%"><p>Spalding</p></td>
                                                                            </tr>
                                                                            <tr>
                                                                              <td><p>Lincoln</p></td>
                                                                              <td><p>Corby</p></td>
                                                                            </tr>
                                                                            <tr>
                                                                              <td><p> Spilsby</p></td>
                                                                              <td><p>Retford</p></td>
                                                                            </tr>
                                                                          </tbody>
                                                                        </table></td>
                                                                      <td valign="top" width="79%"><p><strong>Address<br />
                                                                        <br />
                                                                      </strong>Chandlers                                                  Oil &amp; Gas, Warren                                                  Way, Alma Park,                                                  Grantham, Lincolnshire,                                                  NG31 9SE<br />
                                                                        <br />
                                                                        <strong>TEL: </strong>08456                                                  202010 <strong>FAX:</strong> 01476                                                  568147 <strong>E-Mail: </strong><a href="mailto:websitesales@chandlersoil.com">websitesales@chandlersoil.com</a></p>
                                                                        <table border="0" cellpadding="0" cellspacing="0" width="100%">
                                                                          <tbody>
                                                                            <tr>
                                                                              <td><p align="right"> </p></td>
                                                                            </tr>
                                                                          </tbody>
                                                                        </table></td>
                                                                    </tr>
                                                                  </table>
                                                                </div>                                                              
                              </div>
                              </body>
                              </html>
                              ';
         $textBody = "$templateData->mailinglistgroupmessage";


              // instantiate the class
              $mailer = new FreakMailer();

              // Get the user's Email
              $sql = mysql_query("SELECT leadname,businessname,email,mailtype FROM hqfjt_chronoforms_data_addupdatelead WHERE keromailinglist='$kerouserTemplate' AND dervmailinglist='$dervuserTemplate' AND gasoilmailinglist='$gasoiluserTemplate'");

              while($row = mysql_fetch_object($sql))
              {
                  // Send the emails in this loop.
                  $name = $row->leadname;
                   $businessname = $row->businessname;
                    $to_email = $row->email;
                    $mailtype = $row->mailtype;
                  if(!empty($row->businessname))
                  {
                      $name .= ' '.$row->leadname;
                  }
                  $to_name = $name;

                  if($row->mailtype == 'html')
                  {
                      $mailer->Body = str_replace('{name}', $name, $htmlBody);
      // the line below is the new one I have added
                      $mailer->Body = str_replace('{fuel}', $templatename, $htmlBody);
                      $mailer->isHTML(true);
                      $mailer->AltBody = str_replace('{name}', $name, $textBody);
                      $mailer->AddAddress($to_email, $name);
                      $mailer->Subject = "Your Fuel Prices From Chandlers Oil & Gas";
                      $mailer->FromName = "Chandlers Oil & Gas";
                  }
                  else
                  {
                      $mailer->Body = str_replace('{name}', $name, $textBody);
    // the line below is the new one I have added
                       $mailer->Body = str_replace('{fuel}', $templatename, $textBody);
                      $mailer->isHTML(false);
                      $mailer->Subject = "Your Fuel Prices From Chandlers Oil & Gas";
                      $mailer->FromName = "Chandlers Oil & Gas";
                          $mailer->AddAddress($to_email, $name);                                  
                  }

                  $mailer->Send();
                  $mailer->ClearAddresses();
                  $mailer->ClearAttachments();
                  $mailer->IsHTML(false);
                  echo "Mail sent to: $name - $to_email<br />";
              }

              ?>

【问题讨论】:

    标签: php mysql html preg-replace str-replace


    【解决方案1】:

    我会先试试这个:

    $mailer->Body = str_replace(array('{name}', '{fuel}'), array($name, $templatename), $textBody);
    

    来自docs

    如果搜索和替换是数组,那么 str_replace() 取一个值 从每个数组中,并使用它们来搜索和替换主题。如果 replace 的值比 search 少,则使用空字符串 其余的重置价值。如果 search 是一个数组并且 replace 是一个 字符串,则此替换字符串用于 搜索。但是,反过来就没有意义了。

    如果搜索或替换是数组,它们的元素首先被处理为 最后。

    【讨论】:

    • 感谢您的帮助,效果很好,我只需要将模板名改为 $templatename,但除此之外它还可以与上述内容一起使用 :-)
    【解决方案2】:

    问题是你的代码:

    //Here you set $mailer->Body = $htmlBody (while replacing {name})
    $mailer->Body = str_replace('{name}', $name, $htmlBody);
    // the line below is the new one I have added
    //Here you set $mailer->Body = $htmlBody (while replacing {fuel})
    $mailer->Body = str_replace('{fuel}', $templatename, $htmlBody);
    

    问题是你在第二个str_replace() 中覆盖了 $mailer->Body 你需要做的是使用 $mailer->Body 而不是 $htmlBody

    $mailer->Body = str_replace('{fuel}', $templatename,$mailer->Body);
    

    更好的解决方案是上面发布的(使用带有 str_replace() 的数组),但我想指出代码中的实际错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-26
      • 2017-09-30
      • 1970-01-01
      • 2012-12-03
      • 1970-01-01
      相关资源
      最近更新 更多