2012-12-12 85 views
0

我需要在頁面上的所有內容上運行'preg_replace',包括內部小部件內容,頁眉頁腳和顯示的所有其他位置。我怎樣才能瀏覽頁面上的所有輸出?有沒有辦法過濾頁面上的所有內容?

我想要替換頁面上可能發生的所有不良詞。我只是不知道如何搜索頁面上的所有內容。

+1

你確定這真的是你需要* *做什麼? – alex

+0

是的。我確定。 – user1082764

回答

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); 
相關問題