【问题标题】:PHP : replace character – followed with several non breaking spaces in wordpress "the_content"PHP:替换字符 – 在 wordpress "the_content" 中后跟几个非中断空格
【发布时间】:2019-05-04 06:30:06
【问题描述】:

在 Wordpress 的自定义模板中,我有很多这样的行:

<h3>
<a name="_Toc531441816"></a> == 0
"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.2.1 -&nbsp;&nbsp;&nbsp;&nbsp;Blabla..."
</h3>

有一个字符–和四个字符&amp;nbsp;
此代码来自 Word 的导出。

行首不用改,但需要替换

&nbsp;&nbsp;`3.2.1 -&nbsp;&nbsp;&nbsp;&nbsp;Blabla...

与

&nbsp;&nbsp;`3.2.1 - Blabla...

我认为我必须使用正则表达式,但我没有找到正确的表达式。
我找不到如何搜索表达式 -&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 以将其替换为 -

谢谢你的帮助,
问候, 布鲁诺

【问题讨论】:

    标签: html regex wordpress


    【解决方案1】:

    如果要替换的字符串正好是–&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;,则不需要正则表达式,直接搜索替换即可。

    但是,正则表达式应该是 –(?:&amp;nbsp;){4}

    【讨论】:

    • 其实我在function.php中使用的代码是:function replace_text_wps($str){ $str = preg_replace('/-/','+', $str);返回 $str; } add_filter('the_content', 'replace_text_wps');没有什么变化。我认为我必须用 unicode 替换 '-'。我尝试了 chr(150) 没有结果。我不知道该怎么办...
    • 可能是em dash?
    • 也许,我不知道。事实上,我试过 chr(8212) => 不工作。你知道“preg_replace”的正确表达方式吗?
    • 我找到了替换“-”的代码:–我找到了替换“”的代码:\xc2\xa0 我用这个代码替换了“–”: function replace_text_wps($str){ $str = mb_ereg_replace("–", "+", $str );返回 $str; } add_filter('the_content', 'replace_text_wps');你能帮我修正一下“-”的表达吗(- 后跟 4 个不可破坏的空格)。其实如何连接:"–" "\xc2\xa0" "\xc2\xa0" "\xc2\xa0" "\xc2\xa0" 谢谢你的帮助,
    • 正确的表达式是:$str = mb_ereg_replace('–(?:\xc2\xa0){4}', '+', $str );
    【解决方案2】:

    将字符串“–”和“●”替换为“–”和“●”的正确表达式是: 对于“不可破坏的空间”: $str = mb_ereg_replace('–(?:\xc2\xa0){4}', '– ', $str ); 对于“黑圈”: $str = mb_ereg_replace('\xE2\x97\x8F(?:\xc2\xa0){4}', '● ', $str );

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2012-03-20
      • 2015-08-07
      • 2012-06-04
      • 2016-06-17
      • 2014-01-18
      • 2019-12-22
      相关资源
      最近更新 更多