Detailed usage of ob_get_contents (), ob_end_clean (), ob_start ()

Source: Internet
Author: User

Ob_get_contents ();
Ob_end_clean ();
Ob_start ()

Use ob_start () to output the same output to the buffer, rather than to the browser.
Then, use ob_get_contents to obtain the data in the buffer.
Ob_start () opens a buffer on the server to save all the output. Therefore, when echo is used at any time, the output will be added to the buffer until the program running ends or the end ends with ob_flush. Then the buffer content in the server will be sent to the browser for resolution and display by the browser.

The ob_end_clean function clears the buffer and closes the buffer, but does not output the content.
In this case, you must use the ob_get_contents () function to obtain the buffer content before ob_end_clean.
In this way, you can save the content to a variable before executing ob_end_clean (), and then perform operations on the variable after ob_end_clean.

This is EG:
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;
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 CT that ob_get_contents will return changed ob.
It will work as you wocould use ob_start with no parameter at all. So don't be 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 $ 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 ' ';
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. A simple ob_get_clean () example

Copy codeThe Code is as follows: <? Php
Ob_start ();
Echo "Hello World ";
$ Out = ob_get_clean ();
$ Out = strtolower ($ out );
Var_dump ($ out );
?>

Our example will output: string (11) "hello world"
See also ob_start () and ob_get_contents ().
Notice that the function beneath does not catch errors, so throw in an @ before those ob _ * cballs
Running PHP4 <4.3.0, you can simply add the following to use the function anyway:Copy codeThe Code is 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.