2010-10-31 32 views
4

我允許在我的php文件中有兩個或更多的ob_start();如果是這樣,結束一個ob_start();並開始另一個的正確方法是什麼?PHP ob_start()問題

回答

6

從手冊:

輸出緩衝器是可堆疊的,即, 可以撥打ob_start(),而另一個 ob_start()是有效的。只要確保 您調用ob_end_flush()函數的次數 適當數量。如果 多輸出回調函數是 活性,輸出被在 嵌套順序過濾 依次經由他們每個人。

除了堆疊(嵌套)之外,還可以按順序分開放置塊。

<? 
ob_start(); 
echo "Foo"; 
ob_end_flush(); // outputs buffer contents and turns off output buffering 

ob_start(); 
echo "Bar"; 
ob_end_flush(); 
?> 
0

您可以在頁面上執行多個ob_start()。用ob_end_clean()結束ob_start()。

ob_start(); 
$postOutput = preg_replace('/<img[^>]+./','', ob_get_contents()); 
ob_end_clean(); 
echo $postOutput; 
+1

如果你沒有做任何額外的處理'$ postOutput'然後'ob_end_clean()'只是相比'ob_end_flush做額外的工作()' – 2010-10-31 01:26:54

+0

權利,因爲ob_end_clean會刪除上緩衝。 – matthewpavkov 2010-10-31 01:30:18