如何删除具有变量的特定文本字符串

How do I remove a specific string of text that has variables

我正在使用 wordpress 网站。我想删除或替换一些文本。这是来自插件的内容我无法更改输出(至少从我的所有尝试来看)。

例如带括号;

(文本:date1⁠–date2)

(正文:Feb-09-Feb-12)

这两个日期是可变的,因为它们会一直变化。有没有一种方法可以设置通配符来表示 data1 和 data2 中的任何内容,用 X 替换字符串 date1-date2 或完全删除整个字符串“(The Text: date1⁠–date2)”。

有什么功能可以添加到我的主题 function.php 文件中吗?

或者使用 css 作为替代方案。此文本位于我可以设置为 disply:none 的 div 中,但在其中我需要另一个 div 才能显示。我的想法是只删除文本,如上所述。同样,这是来自插件的内容,我无法更改输出(至少从我的所有尝试来看)。我尝试了可见性,但它留下了该文本所在的 space。

如何在设置为 display:none 的 div 中显示内容?

所以

<div style="display:none">
  <div>
    display something
  </div>
</div>

您不能在设置为 display: none; 的元素内显示某些内容。您可以尝试将元素的 font-size 设置为 0,并使用 :after 选择器将您的文本添加到 content 并将 font-size 设置回可读的内容:

div { font-size: 0; }
div:after { font-size: 16px; content: "This Text is shown"; }
<div>(The Text: date01-date02)</div>

或者,它有点像“用锤子打苍蝇”的解决方案,但是对缓冲区中的内容使用 template_redirect hook, you can turn on Output Buffering and run preg_replace(),这是一种我喜欢称之为“运行时查找和替换”的技术。

add_action( 'template_redirect', 'global_find_replace', 99 );
function global_find_replace(){
    ob_start( function( $buffer ){
        $text   = 'Your Text Here';
        $buffer = preg_replace('/\(The Text: .*\)/', $text, $buffer );

        return $buffer;
    });
}

文档和函数参考

Function Linked Description
template_redirect Fires before determining which template to load.
preg_replace() Perform a regular expression search and replace