登录  /  注册

关于ob_get_contents(),ob_end_clean(),ob_start(),的具体用法详解_php技巧

php中文网
发布: 2016-05-17 08:59:21
原创
783人浏览过

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 '关于ob_get_contents(),ob_end_clean(),ob_start(),的具体用法详解_php技巧';
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

复制代码 代码如下:

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
复制代码 代码如下:

if (!function_exists("ob_get_clean")) {
function ob_get_clean() {
$ob_contents = ob_get_contents();
ob_end_clean();
return $ob_contents;
}
}
?>
智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号