【问题标题】:file_get_contents HTML + str_replace not replacing certain values (not apostrophe or special char issue!)file_get_contents HTML + str_replace 不替换某些值(不是撇号或特殊字符问题!)
【发布时间】: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%>&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);

} 

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

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

我可能在这里疯了?

感谢任何提示,希望只是我。

【问题讨论】:

  • 什么是“第一”和“第二”部分?验证问题的正确测试输入数据在哪里?
  • 添加 cmets 以显示替换的第一和第二部分,还添加了通过 file_get_contents 接收的相关 HTML 示例。
  • 您是否验证过&lt;&gt; 实际上是相关位置的那些字符?也许不是 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


【解决方案1】:

这是罪魁祸首:

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

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

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

$html_content 的初始化需要在循环之前进行,然后在循环内你需要不断替换 $html_content 的 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);

}

【讨论】:

  • 当然,我完全搞砸了,今天看来我太累了,无法思考。初学者的错误就在那里。谢谢好心的先生,已经丢了
猜你喜欢
  • 2014-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-22
  • 1970-01-01
相关资源
最近更新 更多