我使用原始方法替换准备好的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);
}
为什么第二部分替换而第一部分不替换?我更改了第二部分替换字符串,但它们是&lt;%text1%&gt;之前,和第一部分一样,它仍然有效。
$ locations和$ questions_answers变量是数组,数据看起来很好,因为它在DEBUG.txt中打印正如我所期望的那样。
我可能在这里失去理智?
任何提示都很受欢迎,希望它只是我。
答案 0 :(得分:1)
这句话应该归咎于:
$html_content = str_replace($str_to_replace_q, $qa['q'], $mail_template_string);
你在循环中有这个,但$mail_template_string
仍然是每次迭代的原始值。
您可以在第一次循环迭代中将X
替换为A
中的XYZ
,从而为您提供AYZ
。
现在您要将Y
替换为B
- 但您需要在已修改的值AYZ
上执行此操作。您的代码目前没有,但会再次将Y
替换为原始数据B
中的XYZ
,因此您现在获得XBZ
,并且看起来像第一次替换没有发生......
$ html_content的初始化需要在循环之前进行,然后在循环内部需要继续替换$ html_content的当前内容。
// initialize variable with the original data before loop
$html_content = $mail_template_string;
// 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'], $html_content );
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);
}