关于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 配置open_basedir 让各虚拟站点独立运行
Nov 12 PHP
Notice: Undefined index: page in E:\PHP\test.php on line 14
Nov 02 PHP
PHP判断远程图片或文件是否存在的实现代码
Feb 20 PHP
Laravel 4 初级教程之Pages、表单验证
Oct 30 PHP
一个经典的PHP验证码类分享
Nov 18 PHP
php在apache环境下实现gzip配置方法
Apr 02 PHP
php判断文件夹是否存在不存在则创建
Apr 09 PHP
PHP  Yii清理缓存的实现方法
Nov 10 PHP
关于 Laravel Redis 多个进程同时取队列问题详解
Dec 25 PHP
PHP设计模式之注册树模式分析
Jan 26 PHP
PHP5.6.8连接SQL Server 2008 R2数据库常用技巧分析总结
May 06 PHP
thinkphp5框架前后端分离项目实现分页功能的方法分析
Oct 08 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
哪吒敖丙传:新人物二哥敖乙出场 小敖丙奶气十足
2020/03/08 国漫
php字符串截取问题
2006/11/28 PHP
php 用checkbox一次性删除多条记录的方法
2010/02/23 PHP
php下拉选项的批量操作的实现代码
2013/10/14 PHP
php微信公众号开发之关键词回复
2018/10/20 PHP
JavaScript版代码高亮
2006/06/26 Javascript
js 金额格式化来回转换示例
2014/02/23 Javascript
JavaScript插件化开发教程 (三)
2015/01/27 Javascript
js实现div拖动动画运行轨迹效果代码分享
2015/08/27 Javascript
Bootstrap组件学习之导航、标签、面包屑导航(精品)
2016/05/17 Javascript
学习Javascript闭包(Closure)知识
2016/08/07 Javascript
Node.js使用gm拼装sprite图片
2017/07/04 Javascript
jquery+css3实现熊猫tv导航代码分享
2018/02/12 jQuery
了解javascript中let和var及const关键字的区别
2019/05/24 Javascript
js实现碰撞检测
2021/01/29 Javascript
[01:25:33]完美世界DOTA2联赛PWL S3 INK ICE vs Magma 第二场 12.20
2020/12/23 DOTA
pip 错误unused-command-line-argument-hard-error-in-future解决办法
2014/06/01 Python
python 爬虫出现403禁止访问错误详解
2017/03/11 Python
Python批量更改文件名的实现方法
2017/10/29 Python
详解Python中的Numpy、SciPy、MatPlotLib安装与配置
2017/11/17 Python
在Python程序员面试中被问的最多的10道题
2017/12/05 Python
selenium+python自动化测试之鼠标和键盘事件
2019/01/23 Python
python删除文件夹下相同文件和无法打开的图片
2019/07/16 Python
python监控nginx端口和进程状态
2019/09/06 Python
Scrapy框架介绍之Puppeteer渲染的使用
2020/06/19 Python
Python把图片转化为pdf代码实例
2020/07/28 Python
无谷物狗粮:Pooch & Mutt
2018/05/23 全球购物
编写类String的构造函数、析构函数和赋值函数
2012/05/29 面试题
关于读书的演讲稿600字
2014/08/27 职场文书
2014年外联部工作总结
2014/11/17 职场文书
三八妇女节寄语
2015/02/27 职场文书
综合素质评价个性与发展自我评价
2015/03/06 职场文书
2015年医院创卫工作总结
2015/04/22 职场文书
5行Python代码实现一键批量扣图
2021/06/29 Python
MYSQL 表的全面总结
2021/11/11 MySQL
面试提问mysql一张表到底能存多少数据
2022/03/13 MySQL