日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php ob_get_contents,ob_get_contents();用法【转】

發布時間:2023/12/31 php 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php ob_get_contents,ob_get_contents();用法【转】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

下面3個函數的用法

ob_get_contents();

ob_end_clean();

ob_start()

使用ob_start()把輸出那同輸出到緩沖區,而不是到瀏覽器。

然后用ob_get_contents得到緩沖區的數據。

ob_start()在服務器打開一個緩沖區來保存所有的輸出。所以在任何時候使用echo ,輸出都將被加入緩沖區中,直到程序運行結束或者使用ob_flush()來結束。然后在服務器中緩沖區的內容才會發送到瀏覽器,由瀏覽器來解析顯示。

函數ob_end_clean 會清除緩沖區的內容,并將緩沖區關閉,但不會輸出內容。

此時得用一個函數ob_get_contents()在ob_end_clean()前面來獲得緩沖區的內容。

這樣的話, 能將在執行ob_end_clean()前把內容保存到一個變量中,然后在ob_end_clean()后面對這個變量做操作。

這是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().

例子 1. A simple ob_get_clean() example<?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_* calls

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

if (!function_exists("ob_get_clean")) {

function ob_get_clean() {

$ob_contents = ob_get_contents();

ob_end_clean();

return $ob_contents;

}

}

?>

總結

以上是生活随笔為你收集整理的php ob_get_contents,ob_get_contents();用法【转】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。