开发者

How do I get the ob_start() callback to fire when getting the contents of the buffer?

开发者 https://www.devze.com 2023-03-25 03:27 出处:网络
I\'ve got a script that runs a custom email obfuscation class\'s Obfuscate() funct开发者_运维技巧ion on the content before displaying it, as follows:

I've got a script that runs a custom email obfuscation class's Obfuscate() funct开发者_运维技巧ion on the content before displaying it, as follows:

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

That all works great. However, I've completely rewritten my view architecture, so I need to run the email obfuscation from within a class function and return that string (which then gets echoed). I initially rewrote the above as:

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

Unfortunately, the $this->obfuscator->Obfuscate() callback is not being fired. I have since learned that ob_get_contents() does not fire the callback, but have tried ob_get_clean() & ob_get_flush() to no avail as well.

So, how can I get the contents of the buffer after the callback has been fired?


Of course, I was overlooking the fact that the only reason to use the callback on ob_start() was because I wanted to run Obfuscate() on the content before it was flushed, but if I'm getting that content back I don't need to run a callback! So, not using a callback and just running ob_get_clean()'s results through Obfuscate() does what I wanted. Doh!

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


Change

ob_end_clean();

with

ob_clean() 

Will trigger .

This is the code eg I tried

<?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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号