程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 關於ob_get_contents(),ob_end_clean(),ob_start(),的具體用法詳解

關於ob_get_contents(),ob_end_clean(),ob_start(),的具體用法詳解

編輯:關於PHP編程

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 '<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
復制代碼 代碼如下:
<?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:
復制代碼 代碼如下:
<?php
if (!function_exists("ob_get_clean")) {
function ob_get_clean() {
$ob_contents = ob_get_contents();
ob_end_clean();
return $ob_contents;
}
}
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved