【问题标题】:Stop and return output buffering at the same time同时停止和返回输出缓冲
【发布时间】:2012-10-21 16:08:33
【问题描述】:

一旦我开始使用ob_start() 进行输出缓冲,我想在一个命令中停止输出缓冲并获取其内容(而不将其发送到输出)。 I reviewed the available functions,我对各个功能的理解如下:

                 clear  return  send    stop
ob_clean          x         
ob_end_clean      x                      x
ob_end_flush                      x      x
ob_flush                          x 
ob_get_clean      x        x             x  // should be called ob_get_end_clean
ob_get_contents            x        
ob_get_flush               x      x 

据我所知,没有一个函数可以在停止缓冲后返回输出,所以当我想捕获函数的输出时,我必须分 3 步完成:

$output = ob_get_contents();
ob_end_clean();
return $output;

是我遗漏了什么还是有命令停止并一次性返回输出缓冲区?

【问题讨论】:

  • 你错过了什么,是的。 ob_get_clean 停止,不仅清除,还在缓冲。
  • 是的(我认为它应该被称为ob_get_end_clean
  • ob_get_contents_end_clean() ?那么简而言之ob_get_clean。也许这有助于记住函数名。

标签: php buffer


【解决方案1】:

the manual说:

ob_get_clean()本质上同时执行ob_get_contents()ob_end_clean()

(由我突出显示)。与您的代码比较:

$output = ob_get_contents();
ob_end_clean();

您的商家信息一定漏掉了什么。

【讨论】:

  • 当你有多个级别的输出缓冲时要小心,调用一次函数可能不够。
【解决方案2】:

应该是这个,它甚至在描述中提到它:)

http://www.php.net/manual/en/function.ob-get-clean.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-21
    • 2023-03-20
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多