ob_get_contents

(PHP 4, PHP 5, PHP 7, PHP 8)

ob_get_contents返回输出缓冲区的内容

说明

ob_get_contents ( ) : string

只是得到输出缓冲区的内容,但不清除它。

返回值

此函数返回输出缓冲区的内容,或者如果输出缓冲区无效将返回false

范例

Example #1 A simple ob_get_contents() example

<?php

ob_start
();

echo 
"Hello ";

$out1 ob_get_contents();

echo 
"World";

$out2 ob_get_contents();

ob_end_clean();

var_dump($out1$out2);
?>

以上例程会输出:

string(6) "Hello "
string(11) "Hello World"

参见

User Contributed Notes

Mark Clements 03-Oct-2014 08:38
Note that, unlike PHP variables which make efficient use of memory, getting the contents of the output buffer results in a new copy of the buffer being made.  This can end up massively increasing the memory requirements of your script if your output is large.

Example code:

<?php

print(memory_get_usage() . "<br>");

ob_start();

for (
$i = 0; $i < 5000000; $i++)
    print(
" ");

print(
memory_get_usage() . "<br>");

$foo = ob_get_contents();

print(
memory_get_usage() . "<br>");

ob_end_flush();

?>

Output:

  351664
 5359152
10359256
ipfound at gmail dot com 01-Oct-2014 07:13
The ob_get_contents() function has different return behaivor in PHP 5.3.x and PHP 5.5.x. Take a look at very simple example for PHP 5.3

<?php
var_dump
(ob_get_contents());
?>
Output: bool(false)

but in PHP 5.5.x, the same php code will return:
Output: string (0) ""

Actually, we can use type regulation - intval(ob_get_contents()), for cheking On/Off output buffering, but it lays the possible problems in the future.
DrDreistein at dontknow dot at 04-Oct-2011 04:07
i made a simple test which is faster: adding some text in the normal way or by the ob stack:

<?php
$start
= microtime(true);
$var = '';
for(
$i=0 ; $i <= 1000000 ; $i++) {
   
$var .= ' ';
}
$middle = microtime(true);

ob_start();
for(
$i=0 ; $i <= 1000000 ; $i++) {
    echo
' ';
}
$var = ob_get_contents();
ob_end_clean();

$end = microtime(true);

echo
$middle - $start;
echo
'<br />';
echo
$end - $middle;
?>

Outputs something like this:

0.33658003807068
0.30778789520264
mauroasprea+php at gmail dot com 30-Dec-2010 08:10
This is an example of how the stack works:

<?php
//Level 0
ob_start();
echo
"Hello ";

//Level 1
ob_start();
echo
"Hello World";
$out2 = ob_get_contents();
ob_end_clean();

//Back to level 0
echo "Galaxy";
$out1 = ob_get_contents();
ob_end_clean();

//Just output
var_dump($out1, $out2);
?>
m227 ( at ) poczta dot onet dot pl 24-Feb-2005 06:53
if you use ob_start with a callback function as a parameter, and that function changes ob string (as in example in manual) don't expect that ob_get_contents will return changed ob.
it will work as you would use ob_start with no parameter at all. So don't be confused.
http://www.francescosaf.altervista.org 08-Feb-2005 02:04
transfer image, another method (alternative to fsockopen or function socket) :

server(192.168.0.1)
makeimage.php
       ...........
       ...........
       $nameimage="xxxx.jpg"
       $comand=exec("plotvelocity.sh $nameimage $paramater1 $paramater2");
       ob_start();
        readfile($nameimage);
        $image_data = ob_get_contents();
        ob_end_clean();
        echo $image_data;
        unlink($nameimage);

Client  (192.168.0.2)
  $bild="images/newimage2.gif";
  $host="192.168.0.1";
  $url=file_get_contents("http://$host/makeimage.php?$querystring");
  $fp = fopen("$bild", 'wb');
  fwrite($fp, $url);
  fclose($fp);
  echo '<img src="'.$bild.'">';

naturally you can transfer whichever thing and not only images