file_get_contents HTML + str_replace 不替换某些值(不是撇号或特殊字符问题!)

file_get_contents HTML + str_replace not replacing certain values (not apostrophe or special char issue!)

我正在使用原始方法替换准备好的 HTML 电子邮件模板 (utf8) 中的某些字符串。我调试了好几个小时,我已经疯了,所以我在这里发布了一个问题。

我检查了这些建议 questions/answers 但 none 似乎回答了我的问题:

要替换的 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%>&nbsp;</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%>&nbsp;</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%>&nbsp;</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%>&nbsp;</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%>&nbsp;</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%>&nbsp;</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%>&nbsp;</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%>&nbsp;</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%>&nbsp;</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%>&nbsp;</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: &quot;Helvetica Neue&quot;, 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: &quot;Helvetica Neue&quot;, 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: &quot;Helvetica Neue&quot;, 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: &quot;Helvetica Neue&quot;, 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: &quot;Helvetica Neue&quot;, 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);

} 

为什么第二部分被替换而第一部分没有?我更改了第二部分替换字符串,但它们之前是 <%text1%> ,所以与第一部分相同,它仍然有效。

$locations 和 $questions_answers 变量是数组,数据看起来很好,因为它打印在 DEBUG.txt 中,正如我所期望的那样。

我可能在这里失去了理智?

任何提示表示赞赏,希望它只是我。

这一行是罪魁祸首:

    $html_content = str_replace($str_to_replace_q, $qa['q'], $mail_template_string);

你的循环中有这个,但 $mail_template_string 仍然是每次迭代的 原始 值。

您在第一个循环迭代中将 XYZ 中的 X 替换为 A,从而得到 AYZ.
现在您想用 B 替换 Y - 但您需要在 已修改的 AYZ 上执行此操作。您的代码目前没有,但再次将原始数据 XYZ 中的 Y 替换为 B,因此您现在得到 XBZ,它 看起来 就像第一次替换没有发生一样...

$html_content的初始化需要在循环之前进行,然后在循环里面需要不断替换$[=41的current里面的内容=].

// 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);

}