有没有办法过滤页面上的所有内容?

时间:2012-12-12 05:01:44

标签: wordpress

我需要在页面上的所有内容上运行'preg_replace',包括内部窗口小部件内容,页眉页脚和显示的所有其他位置。如何在页面上浏览所有输出?

我想要替换页面上可能出现的所有坏词。我只是不知道如何搜索页面上的所有内容。

1 个答案:

答案 0 :(得分:0)

在整个内容上运行preg_replace在性能方面成本极高,而且我不确定要考虑什么,但有一种方法可以做到。

最好的方法是使用输出缓冲区  将所有内容都放入变量中,然后根据需要调整所有内容, 然后输出最终结果。

它看起来像这样:

// starts the output buffer
ob_start(); 

// In your case this is where all the content output is going to go. This echo is just a sample
echo "badword\n"; 

// get all the buffered content output
$OB = ob_get_contents(); 

// clean the buffered content, and also end the output buffer 
ob_end_clean(); 

// run a replace on all the buffered content, and echo it out
echo preg_replace("/badword/", "BEEP", $OB); 
相关问题