CakeFest 2024: The Official CakePHP Conference

ob_get_clean

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

ob_get_cleanアクティブな出力バッファの内容を取得し、そのバッファをオフにする

説明

ob_get_clean(): string|false

この関数は、 (PHP_OUTPUT_HANDLER_CLEANPHP_OUTPUT_HANDLER_FINAL フラグを指定して) 出力ハンドラをコールし、その戻り値を破棄するとともに、 アクティブな出力バッファの内容を返しつつ、 かつそれをオフにします。

PHP_OUTPUT_HANDLER_REMOVABLE を指定して アクティブな出力バッファを開始しないと、 ob_get_clean() は失敗します。

ob_get_clean() は、PHP_OUTPUT_HANDLER_CLEANABLE を指定せずにアクティブな出力バッファを開始したとしても、 そのバッファの内容をクリアします。

パラメータ

この関数にはパラメータはありません。

戻り値

成功した場合、アクティブな出力バッファの内容を返します。 失敗した場合は、false を返します。

警告

アクティブな出力バッファがない場合、  ob_get_clean()false を返しますが E_NOTICE は発生しません。

エラー / 例外

この関数が失敗すると、 E_NOTICE が発生します。

例1 単純な ob_get_clean() の例

<?php

ob_start
();

echo
"Hello World";

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

var_dump($out);
?>

上の例の出力は以下となります。

string(11) "hello world"

参考

  • ob_start() - 出力のバッファリングを有効にする
  • ob_get_contents() - 出力用バッファの内容を返す
  • ob_clean() - アクティブな出力バッファの内容をクリア(消去)する
  • ob_end_clean() - アクティブな出力用バッファをクリア(消去)し、出力のバッファリングをオフにする
  • ob_get_flush() - アクティブな出力ハンドラの戻り値をフラッシュ(送信)し、その内容を文字列として返した後で、それをオフにする

add a note

User Contributed Notes 6 notes

up
75
geo dot artemenko at gmail dot com
10 years ago
The definition should mention that the function also "turns off output buffering", not just cleans it.
up
31
steven at bielik dot com
13 years ago
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 ...
up
7
paul+phpnet at earth2me dot com
10 years ago
Keep in mind that output may be buffered by default, depending on how you are running PHP (CGI, CLI, etc.). You can use ob_get_level() to determine if an output buffer has already been started. On most web servers I've used, output buffering is already one level deep before my scripts start running.

You should only end as many output buffers as you start. Assuming that your buffer is always the first buffer, or otherwise closing pre-existing buffers, could lead to problems. In PHP 5.5, you can ensure that output buffers are ended properly using a try-finally block.

Something like this is almost guaranteed to break stuff:

<?php
// Don't ever do this!
while (ob_get_level() > 1)
{
ob_end_flush();
}

$content = ob_get_clean();
?>

The problem is that number, "1". Using a fixed number there is asking for trouble. Instead, use ob_get_level() to get the number of output buffers applied when your code starts, and return to that number, if you really must use an unknown number of output buffers:

<?php
ob_start
();
$saved_ob_level = ob_get_level();

// Do stuff here:
run_something();

// If you really must close all of your output buffers except one, this'll do it:
while (ob_get_level() > $start_ob_level)
{
ob_end_flush();
}

// And now, the final output buffer that belongs to us:
$content = ob_get_clean();
?>
up
0
sergei dot solomonov at gmail dot com
11 years ago
<?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)
up
-3
Peter Smartt
6 years ago
I was trying to debug my code using error_log() and I discovered that ob_get_clean() also truncates the error_log() buffer right in the middle of its output, and well as the output buffer which it is supposed to truncate. If you are using error_log(), use ob_get_contents() and ob_end_clean() instead of ob_get_clean().
up
-45
ludvig dot ericson at gmail dot com
18 years ago
Notice that the function beneath does not catch errors, so throw in an @ before those ob_* calls
To Top