2011-08-03 28 views
0

我有一個運行自定義電子郵件混淆類的Obfuscate()上顯示之前的內容功能的腳本,內容如下:如何獲取緩衝區內容時觸發ob_start()回調?

ob_start(array($obfuscator, "Obfuscate")); 
include('header.php'); 
print($html); 
include('footer.php'); 
ob_end_flush(); 

這一切的偉大工程。但是,我完全重寫了我的視圖體系結構,所以我需要從類函數內部運行電子郵件模糊處理並返回該字符串(然後獲取echo)。我最初將上面重寫爲:

ob_start(array($this->obfuscator, "Obfuscate")); 
include('header.php'); 
echo($this->content); 
include('footer.php'); 
$wrappedContent = ob_get_contents(); 
ob_end_clean(); 

不幸的是,$this->obfuscator->Obfuscate()回調未被觸發。我已經知道ob_get_contents()不會觸發回調,但已嘗試ob_get_clean() & ob_get_flush()也無濟於事。

那麼,如何在回調被觸發後獲取緩衝區的內容呢?

回答

3

當然,我俯瞰的事實,唯一的理由使用回叫上ob_start()是因爲我想在內容上運行Obfuscate()它刷新之前,但如果我收到內容後,我不要」不需要運行回調!因此,不使用回調,只是運行ob_get_clean()的結果通過Obfuscate()做我想要的。衛生署!

ob_start(); 
include('header.php'); 
echo($this->content); 
include('footer.php'); 
return $this->obfuscator->Obfuscate(ob_get_clean()); 
+0

謝謝,我有同樣的問題;-) – acme

+0

我太感謝了!有時你需要側着腳去看看明顯的解決方案 – Paolo

0

變化

ob_end_clean(); 

ob_clean() 

將觸發。

這是代碼如我試過

<?php 
class Obfuscate { 
    public function __construct() 
    { 

    } 

    public function getObfuscate() 
    { 
     return "obfuscate"; 
    } 
} 

class Example 
{ 
    public function hello($obfuscator) 
    { 
     ob_start(array($obfuscator, 'getObfuscate')); 
     include('header.php'); 
     echo "Thi is a content "; 
     include('footer.php'); 
     $wrappedContent = ob_get_contents(); 
     ob_clean(); 
    } 
} 

$obfuscator = new Obfuscate(); 
$example = new Example; 
$example->hello($obfuscator); 
ob_clean(); 
+0

如何在ob_get_contents()不調用回調函數後調用ob_clean()函數? – morgant

+0

我不知道:( –

相關問題