關(guān)于ob_get_contents(),ob_end_clean(),ob_start(),的具體用法詳解
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
<?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;
}
}
?>
相關(guān)文章
php多線程實(shí)現(xiàn)方法及用法實(shí)例詳解
這篇文章主要介紹了php多線程實(shí)現(xiàn)方法及用法實(shí)例,PHP多線程實(shí)現(xiàn)方法和fsockopen函數(shù)有關(guān),需要的朋友可以參考下2015-10-10
php expects parameter 1 to be resource, array given 錯(cuò)誤
從名字可以看出來(lái)這是說(shuō)你傳的參數(shù)有問(wèn)題,也就是說(shuō),你在定義傳遞參數(shù)的時(shí)候 例如 mysql_query($query,$result),這兩個(gè)參數(shù),你只用了$query這個(gè)參數(shù),那么$result這個(gè)參數(shù),沒(méi)有使用,就會(huì)報(bào)這個(gè)錯(cuò)誤2011-03-03
PHP簡(jiǎn)單開啟curl的方法(測(cè)試可行)
這篇文章主要介紹了PHP簡(jiǎn)單開啟curl的方法,較為詳細(xì)的講述了PHP開啟curl函數(shù)庫(kù)的具體步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-01-01
WordPress中用于獲取搜索表單的PHP函數(shù)使用解析
這篇文章主要介紹了WordPress中用于獲取搜索表單的PHP函數(shù)使用解析,即get_search_form函數(shù)的基本用法,需要的朋友可以參考下2016-01-01
php采集文章中的圖片獲取替換到本地(實(shí)現(xiàn)代碼)
本篇文章是對(duì)php采集文章中的圖片獲取替換到本地的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07
PHP通過(guò)串口實(shí)現(xiàn)發(fā)送短信
本文給大家詳細(xì)介紹了,如何使用php通過(guò)串口來(lái)實(shí)現(xiàn)發(fā)送短信的思路以及具體的實(shí)現(xiàn)代碼,十分的實(shí)用,有需要的小伙伴可以參考下。2015-07-07
PHP簡(jiǎn)單實(shí)現(xiàn)DES加密解密的方法
這篇文章主要介紹了PHP簡(jiǎn)單實(shí)現(xiàn)DES加密解密的方法,涉及php中mcrypt_encrypt與mcrypt_decrypt方法的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07

