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


Posted in PHP onJune 24, 2013

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;
}
}
?>
PHP 相关文章推荐
十天学会php之第一天
Oct 09 PHP
兼容性最强的PHP生成缩略图的函数代码(修改版)
Jan 18 PHP
php中根据某年第几天计算出日期年月日的代码
Feb 24 PHP
细谈php中SQL注入攻击与XSS攻击
Jun 10 PHP
CURL状态码列表(详细)
Jun 27 PHP
div li的多行多列 无刷新分页示例代码
Oct 16 PHP
php中的curl_multi系列函数使用例子
Jul 29 PHP
Yii使用find findAll查找出指定字段的实现方法
Sep 05 PHP
PHP中判断文件存在使用is_file还是file_exists?
Apr 03 PHP
PHP统一页面编码避免乱码问题
Apr 09 PHP
php实现用户注册密码的crypt加密
Jun 08 PHP
PHP文件系统管理(实例讲解)
Sep 19 PHP
关于php操作mysql执行数据库查询的一些常用操作汇总
Jun 24 #PHP
解析crontab php自动运行的方法
Jun 24 #PHP
解析关于java,php以及html的所有文件编码与乱码的处理方法汇总
Jun 24 #PHP
使用PHP遍历文件目录与清除目录中文件的实现详解
Jun 24 #PHP
探讨:php中在foreach中使用foreach ($arr as &amp;$value) 这种类型的解释
Jun 24 #PHP
PHP中的函数-- foreach()的用法详解
Jun 24 #PHP
解析php框架codeigniter中如何使用框架的session
Jun 24 #PHP
You might like
PHP+MYSQL的文章管理系统(二)
2006/10/09 PHP
深入剖析浏览器退出之后php还会继续执行么
2016/05/17 PHP
IE 当eval遇上function的处理
2011/08/09 Javascript
ASP.NET jQuery 实例13 原创jQuery文本框字符限制插件-TextArea Counter
2012/02/03 Javascript
基于JQuery模仿苹果桌面的Dock效果(初级版)
2012/10/15 Javascript
js中创建对象的几种方式示例介绍
2014/01/26 Javascript
jQuery实现的一个tab切换效果内部还嵌有切换
2014/08/10 Javascript
轻松创建nodejs服务器(9):实现非阻塞操作
2014/12/18 NodeJs
JavaScript数据库TaffyDB用法实例分析
2015/07/27 Javascript
JS实现点击按钮控制Div变宽、增高及调整背景色的方法
2015/08/05 Javascript
angular十大常见问题
2017/03/07 Javascript
vue2项目使用sass的示例代码
2017/06/28 Javascript
js实现图片粘贴上传到服务器并展示的实例
2017/11/08 Javascript
Vue2.0 slot分发内容与props验证的方法
2017/12/12 Javascript
p5.js入门教程之键盘交互
2018/03/19 Javascript
新版小程序登录授权的方法
2018/12/12 Javascript
js实现随机div颜色位置 类似满天星效果
2019/10/24 Javascript
解决iView Table组件宽度只变大不变小的问题
2020/11/13 Javascript
Python标准库之collections包的使用教程
2017/04/27 Python
python使用xlrd和xlwt读写Excel文件的实例代码
2018/09/05 Python
Numpy截取指定范围内的数据方法
2018/11/14 Python
Python正则表达式和元字符详解
2018/11/29 Python
python使用wxpy轻松实现微信防撤回的方法
2019/02/21 Python
Python如何在windows环境安装pip及rarfile
2020/06/15 Python
Python3 webservice接口测试代码详解
2020/06/23 Python
html5使用canvas画一条线
2014/12/15 HTML / CSS
Java中compareTo和compare的区别
2016/04/12 面试题
学校做一个有道德的人活动方案
2014/08/23 职场文书
“四风”查摆问题自我剖析材料
2014/09/27 职场文书
学校拾金不昧表扬信
2015/01/16 职场文书
统计员岗位职责范本
2015/04/14 职场文书
小学数学教师研修日志
2015/11/13 职场文书
少先大队干部竞选稿
2015/11/20 职场文书
市语委办2016年第十九届“推普周”活动总结
2016/04/05 职场文书
MongoDB balancer的使用详解
2021/04/30 MongoDB
Mysql 如何查询时间段交集
2021/06/08 MySQL