Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ob_get_contents (), ob_end_clean (), ob_start () in PHP

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Xiaobian to share with you how to use ob_get_contents(),ob_end_clean(),ob_start() in PHP, I believe most people still don't know how to use, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

ob_get_contents();

ob_end_clean();

ob_start()

Use ob_start() to output that same to a 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 hold all output. So anytime echo is used, the output will be added to the buffer until the program runs out or ob_flush() is used. The contents of the buffer in the server are then sent to the browser, which parses them for display.

The ob_end_clean function clears the contents of the buffer and closes the buffer, but does not output the contents.

In this case, ob_get_contents() is used before ob_end_clean() to obtain the contents of the buffer.

In this case, you can save the contents to a variable before executing ob_end_clean(), and then manipulate this 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 '';

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 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.

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

The copy code is as follows:

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_* calls

Running PHP4 < 4.3.0, you can simply add the following to use the function anyway:

The copy code is as follows:

The above is how to use ob_get_contents(),ob_end_clean(),ob_start() in PHP. Thank you for reading all the contents of this article! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report