获取缓冲区内容时如何触发 ob_start() 回调?
我有一个脚本,可以在显示内容之前运行自定义电子邮件混淆类的 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 echo
ed). 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然,我忽略了一个事实,即在
ob_start()
上使用回调的唯一原因是我想在刷新内容之前对其运行Obfuscate()
,但如果我要取回该内容,我不需要运行回调!因此,不使用回调,只需运行ob_get_clean()< /code> 的结果通过
Obfuscate()
做了我想要的事情。哎哟!Of course, I was overlooking the fact that the only reason to use the callback on
ob_start()
was because I wanted to runObfuscate()
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 runningob_get_clean()
's results throughObfuscate()
does what I wanted. Doh!Will 触发改变
用
。
这是代码,例如我尝试过
Change
with
Will trigger .
This is the code eg I tried