Home > Backend Development > PHP Tutorial > Detailed explanation of usage of ob_get_contents();ob_end_clean();ob_start(); in php

Detailed explanation of usage of ob_get_contents();ob_end_clean();ob_start(); in php

WBOY
Release: 2016-07-25 08:58:29
Original
1319 people have browsed it
This article introduces the specific usage of ob_get_contents(), ob_end_clean(), and ob_start() functions in PHP. Friends in need can refer to it.

php cache related functions: ob_get_contents(); ob_end_clean(); ob_start()

Use ob_start() to output the output to the buffer instead of to the browser. Then use ob_get_contents to get the buffer data.

ob_start() opens a buffer on the server to save all output. Therefore, any time echo is used, the output will be added to the buffer until the program ends or is terminated using ob_flush(). Then the content of the buffer in the server will be sent to the browser, which will be parsed and displayed by the browser.

The function ob_end_clean will clear the contents of the buffer and close the buffer, but will not output the content. At this time, a function ob_get_contents() must be used in front of ob_end_clean() to obtain the contents of the buffer. In this case, the content can be saved to a variable before executing ob_end_clean(), and then the variable can be operated after ob_end_clean().

Example:

<?php
ob_start(); // buf1
echo ' multiple ';
ob_start(); // buf2
echo ' buffers work ';
$buf2 = ob_get_contents();
ob_end_clean();
$buf1 = ob_get_contents();
ob_end_clean();

echo $buf1;
echo '<br/>';
echo $buf2;
?>
Copy after login

Look below, ob_get_contents

(PHP 4, PHP 5) ob_get_contents -- Return the contents of the output buffer Description string ob_get_contents (void)

This will return the contents of the output buffer or FALSE, if output buffering isn't active.

See also ob_start() and ob_get_length().

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 confused work as you would use ob_start with no parameter at all. So don't be. transfer image, another method (alternative to fsockopen or function socket) :

server(192.168.0.1) makeimage.php

<?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.'" alt="Detailed explanation of usage of ob_get_contents();ob_end_clean();ob_start(); in php" >';
?>
Copy after login

naturally you can transfer whichever thing and not only images

ob_get_clean

(PHP 4 >= 4.3.0, PHP 5) ob_get_clean -- Get current buffer contents and delete current output buffer Description string ob_get_clean (void)

This will return the contents of the output buffer and end output buffering. If output buffering isn't active then FALSE is returned. ob_get_clean() essentially executes both ob_get_contents() and ob_end_clean().

Example 1: Simple example of ob_get_clean()

<?php

ob_start();

echo "Hello World";

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

var_dump($out);
?>
Copy after login

Output: string(11) "hello world"

Example 2, ob_start() and ob_get_contents(). Notice that the function beneath does not catch errors, so throw in an @ before those ob_* calls Running PHP4 < 4.3.0, you can simply add the following to use the function anyway: < 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;
}
} //by bbs.it-home.org
?>
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template