获取缓冲区内容时如何触发 ob_start() 回调?

发布于 2024-11-27 13:40:09 字数 696 浏览 0 评论 0原文

我有一个脚本,可以在显示内容之前运行自定义电子邮件混淆类的 Obfuscate() 函数,如下所示:

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() 也无济于事。

那么,在回调被触发后如何获取缓冲区的内容呢?

I've got a script that runs a custom email obfuscation class's Obfuscate() function 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幻想少年梦 2024-12-04 13:40:09

当然,我忽略了一个事实,即在 ob_start() 上使用回调的唯一原因是我想在刷新内容之前对其运行 Obfuscate() ,但如果我要取回该内容,我不需要运行回调!因此,不使用回调,只需运行 ob_get_clean()< /code> 的结果通过Obfuscate() 做了我想要的事情。哎哟!

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

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());
无人问我粥可暖 2024-12-04 13:40:09

Will 触发改变

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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文