Output Control 函数
在线手册:中文  英文

ob_get_clean

(PHP 4 >= 4.3.0, PHP 5)

ob_get_clean得到当前缓冲区的内容并删除当前输出缓。

说明

string ob_get_clean ( void )

得到当前缓冲区的内容并删除当前输出缓冲区。

ob_get_clean() 实质上是一起执行了 ob_get_contents()ob_end_clean()

返回值

返回输出缓冲区的内容,并结束输出缓冲区。如果输出缓冲区不是活跃的,即返回 FALSE

范例

Example #1 A simple ob_get_clean() example

<?php

ob_start
();

echo 
"Hello World";

$out ob_get_clean();
$out strtolower($out);

var_dump($out);
?>

以上例程会输出:


string(11) "hello world"

参见


Output Control 函数
在线手册:中文  英文

用户评论:

sergei dot solomonov at gmail dot com (2012-08-25 16:20:19)

<?php
ob_start
();
echo 
"1";
$content ob_get_clean();

echo 
"2";
$content .= ob_get_clean();

echo 
$content;
?>

This script outputs 21 in CLI mode and 12 otherwise (under my apache and nginx)

steven at bielik dot com (2011-02-21 23:57:36)

Also, don't forget that you will need to ob_start() again for any successive calls:

<?php
ob_start
();
echo 
"1";
$content ob_get_clean();

ob_start(); // This is NECESSARY for the next ob_get_clean() to work as intended.
echo "2";
$content .= ob_get_clean();

echo 
$content;
?>

Output: 12

Without the second ob_start(), the output is 21 ...

56ka dot prog at gmail dot com (2010-09-13 17:27:52)

Hi
Warning, this command isn't exactly like ob_get_contents() and ob_end_clean() successively. Sometimes you will have to do these two commands manually.
Today I lost 2 hours to localize that problem and to find the solution -_-'
See you

profix at cms-studio dot net (2008-07-09 19:01:53)

Maybe it can be useful
$__ob_stack=array();
function ob_break() {
global $__ob_stack;
while (ob_get_level()>0)
array_push($__ob_stack,ob_get_clean());
}
function ob_continue() {
global $__ob_stack;
while (count($__ob_stack)>0) {
ob_start();
echo array_pop($__ob_stack);
}
}
ob_start();
echo "1"; // now we write to "ob" buffer
ob_break();
echo "2"; // now we write to stdout
ob_continue();
echo "3"; // now we write again to "ob" buffer
$output=ob_get_clean();
echo $output;
// output is 213

egbert teeselink (2008-02-02 07:47:29)

If you're trying to capture the output from an included 3rd party script you don't seem to be capturing everything, the following might help:
function ob_end_clean_all()
{
$s = "";
do
{
$s = ob_get_contents() . $s;
} while(ob_end_clean());
return $s;
}
This function closes all nested output bufferings, therefore closing any buffers that the included script does not explicitly close. It's basically a ob_end_clean that sweeps together all the output buffers instead of just the innermost one.

ludvig dot ericson at gmail dot com (2005-08-10 07:10:36)

Notice that the function beneath does not catch errors, so throw in an @ before those ob_* calls

webmaster at ragnarokonline dot de (2003-10-01 17:21:28)

Running PHP4 < 4.3.0, you can simply add the following to use the function anyway:

<?php
if (!function_exists("ob_get_clean")) {
    function 
ob_get_clean() {
        
$ob_contents ob_get_contents();
        
ob_end_clean();
        return 
$ob_contents;
    }
}
?>

易百教程