国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > PHP > 正文

關(guān)于ob_get_contents(),ob_end_clean(),ob_start(),的具體用法詳解

2024-05-04 23:11:20
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

ob_get_contents();
ob_end_clean();
ob_start()

使用ob_start()把輸出那同輸出到緩沖區(qū),而不是到瀏覽器。
然后用ob_get_contents得到緩沖區(qū)的數(shù)據(jù)。
ob_start()在服務(wù)器打開一個(gè)緩沖區(qū)來(lái)保存所有的輸出。所以在任何時(shí)候使用echo ,輸出都將被加入緩沖區(qū)中,直到程序運(yùn)行結(jié)束或者使用ob_flush()來(lái)結(jié)束。然后在服務(wù)器中緩沖區(qū)的內(nèi)容才會(huì)發(fā)送到瀏覽器,由瀏覽器來(lái)解析顯示。

函數(shù)ob_end_clean 會(huì)清除緩沖區(qū)的內(nèi)容,并將緩沖區(qū)關(guān)閉,但不會(huì)輸出內(nèi)容。
此時(shí)得用一個(gè)函數(shù)ob_get_contents()在ob_end_clean()前面來(lái)獲得緩沖區(qū)的內(nèi)容。
這樣的話,能將在執(zhí)行ob_end_clean()前把內(nèi)容保存到一個(gè)變量中,然后在ob_end_clean()后面對(duì)這個(gè)變量做操作。

這是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 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 '<img src="'.$bild.'">';
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

復(fù)制代碼 代碼如下:


<?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:

復(fù)制代碼 代碼如下:


<?php
if (!function_exists("ob_get_clean")) {
function ob_get_clean() {
$ob_contents = ob_get_contents();
ob_end_clean();
return $ob_contents;
}
}
?>

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 陆丰市| 漯河市| 安徽省| 宿迁市| 宁波市| 莱州市| 福海县| 广州市| 汝城县| 尼玛县| 大化| 山丹县| 宝清县| 桦川县| 论坛| 那曲县| 信阳市| 永清县| 资阳市| 东乌珠穆沁旗| 油尖旺区| 宝山区| 洞头县| 延寿县| 竹溪县| 沐川县| 博客| 南安市| 简阳市| 邛崃市| 雷山县| 禄丰县| 英德市| 南和县| 息烽县| 永宁县| 崇文区| 阿巴嘎旗| 青岛市| 蒙山县| 神农架林区|