【发布时间】:2018-04-04 11:49:06
【问题描述】:
我正在使用一种原始方式来替换准备好的 HTML 电子邮件模板 (utf8) 中的某些字符串。我正在调试这个几个小时并且已经发疯了,所以我在这里发布一个问题。
我检查了这些建议的问题/答案,但似乎没有人回答我的问题:
要替换的 HTML 的相关部分(从 file_get_contents 读取):
<ol>
<li><strong><span style="font-family:arial,helvetica,sans-serif;"><span style="color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; text-align: start;"><%q1%>: <em><%a1%> </em></span></span></strong></li>
<li><strong><span style="font-family:arial,helvetica,sans-serif;"><span style="color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; text-align: start;"><%q2%>: <em><%a2%> </em></span></span></strong></li>
<li><strong><span style="font-family:arial,helvetica,sans-serif;"><span style="color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; text-align: start;"><%q3%>: <em><%a3%> </em></span></span></strong></li>
<li><span style="font-family:arial,helvetica,sans-serif;"><span style="color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; text-align: start;"><%q4%>: <strong><em><%a4%> </em></strong></span></span></li>
<li><strong><span style="font-family:arial,helvetica,sans-serif;"><span style="color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; text-align: start;"><%q5%>: <em><%a5%> </em></span></span></strong></li>
<li><strong><span style="font-family:arial,helvetica,sans-serif;"><span style="color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; text-align: start;"><%q6%>: <em><%a6%> </em></span></span></strong></li>
<li><strong><span style="font-family:arial,helvetica,sans-serif;"><span style="color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; text-align: start;"><%q7%>: <em><%a7%> </em></span></span></strong></li>
<li><strong><span style="font-family:arial,helvetica,sans-serif;"><span style="color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; text-align: start;"><%q8%>: <em><%a8%> </em></span></span></strong></li>
<li><span style="font-family:arial,helvetica,sans-serif;"><span style="color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; text-align: start;"><%q9%>: <strong><em><%a9%> </em></strong></span></span></li>
<li><strong><span style="font-family:arial,helvetica,sans-serif;"><span style="color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; text-align: start;"><%q10%>: <em><%a10%> </em></span></span></strong></li>
</ol>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table class="module"
role="module"
data-type="divider"
border="0"
cellpadding="0"
cellspacing="0"
width="100%"
style="table-layout: fixed;">
<tr>
<td style="padding:0px 0px 0px 0px;"
role="module-content"
height="100%"
valign="top"
bgcolor="">
<table border="0"
cellpadding="0"
cellspacing="0"
align="center"
width="100%"
height="10px"
style="line-height:10px; font-size:10px;">
<tr>
<td
style="padding: 0px 0px 10px 0px;"
bgcolor="#d40a0a"></td>
</tr>
</table>
</td>
</tr>
</table>
<table class="module" role="module" data-type="text" border="0" cellpadding="0" cellspacing="0" width="100%" style="table-layout: fixed;">
<tr>
<td style="padding:18px 0px 18px 0px;line-height:22px;text-align:inherit;"
height="100%"
valign="top"
bgcolor="">
<div style="text-align: center;"><span style="color: rgb(77, 77, 79); font-family: arial, helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 300; letter-spacing: 0.4px; background-color: rgba(255, 255, 255, 0.9);"></span></div>
<div>
<ol style="font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; font-family: arial, helvetica, sans-serif; font-size: 14px; color: rgb(212, 10, 10);">
<li style="font-size: 14px;"><strong><span style="font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(51, 51, 51);"><!--%location1%--></span></strong></li>
<li style="font-size: 14px;"><strong><span style="font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(51, 51, 51);"><!--%location2%--></span></strong></li>
<li style="font-size: 14px;"><strong><span style="font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(51, 51, 51);"><!--%location3%--></span></strong></li>
<li style="font-size: 14px;"><strong><span style="font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(51, 51, 51);"><!--%location4%--></span></strong></li>
<li style="font-size: 14px;"><span style="font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(51, 51, 51);"><!--%location5%--></span></li>
</ol>
一部分正在按预期替换,而另一部分则没有,在某些时候它实际上替换了一些字符串,但不是全部。我不确定发生了什么。
相关部分代码:
$html_content = '';
$mail_template_string = file_get_contents(get_template_directory_uri() . '/mail_template_questionnaire.html');
if($mail_template_string) {
$myfile = fopen("DEBUG.txt", "a") or die("Unable to open file!");
// FIRST PART OF REPLACING this doesnt replace, even though DEBUG.txt says it should
foreach($questions_answers as $indx => $qa) {
$i = $indx+1;
$str_to_replace_q = '<%q'.$i.'%>';
$str_to_replace_a = '<%a'.$i.'%>';
$html_content = str_replace($str_to_replace_q, $qa['q'], $mail_template_string);
fwrite($myfile, 'Replace '. $str_to_replace_q . ' with ' . $qa['q']."\n");
fwrite($myfile, 'Replace '. $str_to_replace_a . ' with ' . $qa['a']."\n");
$html_content = str_replace($str_to_replace_a, $qa['a'], $html_content);
}
// SECOND REPLACING PART: this works and replaces as expected
foreach($locations as $indx => $loc) {
$i = $indx+1;
$html_content = str_replace('<!--%location'.$i.'%-->', $loc['name'], $html_content);
}
fwrite($myfile, $qa['q']. ' -> '.$qa['a']."\n --- \n");
fclose($myfile);
}
为什么第二部分被替换而第一部分没有?我更改了第二部分替换字符串,但它们之前是 ,所以与第一部分相同,它仍然有效。
$locations 和 $questions_answers 变量是数组,数据看起来很好,因为它打印在 DEBUG.txt 中,正如我所期望的那样。
我可能在这里疯了?
感谢任何提示,希望只是我。
【问题讨论】:
-
什么是“第一”和“第二”部分?验证问题的正确测试输入数据在哪里?
-
添加 cmets 以显示替换的第一和第二部分,还添加了通过 file_get_contents 接收的相关 HTML 示例。
-
您是否验证过
<和>实际上是相关位置的那些字符?也许不是 HTML 实体……? -
第一部分有
fwrite调用,可能它们不能正常工作? -
$html_content = str_replace($str_to_replace_q, $qa['q'], $mail_template_string);- 在每个循环迭代中,您在此处用您刚刚替换的内容覆盖$html_content,在原始输入数据中,$mail_template_string。您需要使用循环 before 的起始值初始化$html_content,然后继续替换 $html_content 此时已包含的内容。
标签: php str-replace