简体   繁体   English

PHP的str_replace无法正常工作?

[英]PHP's str_replace not working?

I am trying to do a simple task of storing an Email template into a Database, retrieving it and populating variables in the template before sending an email. 我正在尝试做一个简单的任务,将电子邮件模板存储到数据库中,进行检索,并在发送电子邮件之前在模板中填充变量。

I am using str_replace but for some reason it is not working for me. 我正在使用str_replace但是由于某种原因它对我不起作用。 To complicate it more, it works for 1 of my template variables below, the {{date}} one does get populated but all the others get skipped. 更复杂的是,它适用于以下我的模板变量之一, {{date}}确实被填充,而其他所有变量都被跳过。

It is not an issue with my POST variables not being populated, I can even pass in a real text string in str_replace and it will still not find my "template variables" and replace them. 这不是我的POST变量未填充的问题,我什至可以在str_replace传递真实的文本字符串,并且仍然找不到我的“模板变量”并替换它们。 Just the date one. 只是约会之一。

Any ideas why it would have such odd behavior? 有什么想法为什么会有这种奇怪的行为吗? From what I can see, the Date one has nothing different? 从我看,日期一有什么不同?

Template 模板

$admin_template = '<table>
    <tr>
      <td><strong>Name:</strong></td><td>{{name}}</td>
    </tr>
    <tr>
      <td><strong>Email:</strong></td><td>{{email}}</td>
    </tr>
    <tr>
      <td><strong>Website:</strong></td><td>{{website}}</td>
    </tr>
    <tr>
      <td><strong>Phone:</strong></td><td>{{phone}}</td>
    </tr>
    <tr>
      <td><strong>Source:</strong></td><td>{{source}}</td>
    </tr>
    <tr>
      <td><strong>Budget:</strong></td><td>{{budget}}</td>
    </tr>
    <tr>
      <td><strong>IP:</strong></td><td>{{{ip}}}</td>
    </tr>
    <tr>
      <td><strong>Date Submitted:</strong></td><td>{{date}}</td>
    </tr>
    <tr>
      <td><strong>Message:</strong></td><td>{{message}}</td>
    </tr>
  </table>';

Code to Process Template 处理模板的代码

// Process Admin Template
$admin_body = str_replace('{{name}}', $name, $admin_template);
$admin_body = str_replace('{{email}}', $mailFrom, $admin_template);
$admin_body = str_replace('{{phone}}', $_POST["phone"], $admin_template);
$admin_body = str_replace('{{website}}', $_POST["website"], $admin_template);
$admin_body = str_replace('{{message}}', $_POST["message"], $admin_template);
$admin_body = str_replace('{{budget}}', $_POST["budget"], $admin_template);
$admin_body = str_replace('{{source}}', $_POST["source"], $admin_template);
$admin_body = str_replace('{{ip}}', $_SERVER['REMOTE_ADDR'], $admin_template);
$admin_body = str_replace('{{date}}', date("Y-m-d H:i:s"), $admin_template);

Actual Output After Processing 处理后的实际输出

// Actual Output...
<table>
<tr>
  <td><strong>Name:</strong></td><td>{{name}}</td>
</tr>
<tr>
  <td><strong>Email:</strong></td><td>{{email}}</td>
</tr>
<tr>
  <td><strong>Website:</strong></td><td>{{website}}</td>
</tr>
<tr>
  <td><strong>Phone:</strong></td><td>{{phone}}</td>
</tr>
<tr>
  <td><strong>Source:</strong></td><td>{{source}}</td>
</tr>
<tr>
  <td><strong>Budget:</strong></td><td>{{budget}}</td>
</tr>
<tr>
  <td><strong>IP:</strong></td><td>{{{ip}}}</td>
</tr>
<tr>
  <td><strong>Date Submitted:</strong></td><td>2013-05-26 05:01:14</td>
</tr>
<tr>
  <td><strong>Message:</strong></td><td>{{message}}</td>
</tr>
</table>

您一直在从头开始重新创建$admin_body ,放弃先前的str_replace()替换:-D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM