About Ob_get_contents (), Ob_end_clean (), Ob_start (), the specific use of detailed _php tips

Source: Internet
Author: User

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 area data.
Ob_start () opens a buffer on the server to hold all the output. So at any time using echo, the output will be added to the buffer until the program runs out or ends with Ob_flush (). The contents of the buffer in the server are then sent to the browser, which is parsed by the browser.

Function Ob_end_clean clears the contents of the buffer and closes the buffer, but does not output the content.
At this point you have to use a function ob_get_contents () before the Ob_end_clean () to get the contents of the buffer.
In this way, you can save the content in a variable before executing ob_end_clean (), and then do the same with the variable after Ob_end_clean ().

This is eg:
Ob_start (); Buf1
echo ' multiple ';
Ob_start (); Buf2
Echo ' buffers work ';
$buf 2 = ob_get_contents ();
Ob_end_clean ();
$buf 1 = ob_get_contents ();
Ob_end_clean ();
echo $buf 1;
Echo ' <br/> ';
echo $buf 2;
Ob_get_contents

(PHP 4, PHP 5)
Ob_get_contents--Return the contents of the output buffer
Description
String ob_get_contents (void)
This would return the contents of the output buffer or FALSE, if output buffering isn ' t active.
also Ob_start () and Ob_get_length ().
If use Ob_start with a callback function as a parameter, and which function changes OB string (as in example in manual) Don ' t expect that Ob_get_contents would return changed OB.
It'll work as your would use Ob_start with no parameter at all. So don ' t is confused.
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 $paramater 1 $paramater 2");
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 ' ';
Naturally 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 would return the contents to 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. A Simple Ob_get_clean () example

Copy Code code as follows:

<?php
Ob_start ();
echo "Hello World";
$out = Ob_get_clean ();
$out = Strtolower ($out);
Var_dump ($out);
?>

Our example would output:string (one) "Hello World"
also Ob_start () and ob_get_contents ().
Notice that's function beneath does not catch errors, so throw at an @ before those ob_* calls
Running PHP4 < 4.3.0, can simply add the following to use the function anyway:
Copy Code code as follows:

<?php
if (!function_exists ("Ob_get_clean")) {
function Ob_get_clean () {
$ob _contents = ob_get_contents ();
Ob_end_clean ();
return $ob _contents;
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.