关于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读取html并截取字符串的简单代码
Nov 30 PHP
PHP三元运算符的结合性介绍
Jan 10 PHP
PHP随机字符串生成代码(包括大小写字母)
Jun 24 PHP
PHP 线程安全与非线程安全版本的区别深入解析
Aug 06 PHP
ThinkPHP框架任意代码执行漏洞的利用及其修复方法
Jul 04 PHP
PHP图像裁剪缩略裁切类源码及使用方法
Jan 07 PHP
PHP代码维护,重构变困难的4种原因分析
Jan 25 PHP
php 类中的常量、静态属性、非静态属性的区别
Apr 09 PHP
php中请求url的五种方法总结
Jul 13 PHP
php使用curl下载指定大小的文件实例代码
Sep 30 PHP
PHP手机号码及邮箱正则表达式实例解析
Jul 11 PHP
phpQuery解析HTML乱码问题(补充官网未列出的乱码解决方案)
Apr 01 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
摩卡咖啡
2021/03/03 咖啡文化
编译问题
2006/10/09 PHP
相对路径转化成绝对路径
2007/04/10 PHP
JS Timing
2007/04/21 Javascript
初学Javascript的一些总结
2008/11/03 Javascript
基于JQuery的6个Tab选项卡插件
2010/09/03 Javascript
JavaScript中json使用自己总结
2013/08/13 Javascript
深入理解JavaScript系列(25):设计模式之单例模式详解
2015/03/03 Javascript
JS简单实现移动端日历功能示例
2016/12/28 Javascript
JS/HTML5游戏常用算法之路径搜索算法 A*寻路算法完整实例
2018/12/14 Javascript
如何优雅的在一台vps(云主机)上面部署vue+mongodb+express项目
2019/01/20 Javascript
vue实现倒计时获取验证码效果
2020/04/17 Javascript
Vue(定时器)解决mounted不能获取到data中的数据问题
2020/07/30 Javascript
[02:20]DOTA2亚洲邀请赛 EHOME战队出场宣传片
2015/02/07 DOTA
[39:19]完美世界DOTA2联赛PWL S2 SZ vs LBZS 第二场 11.26
2020/11/30 DOTA
python运行其他程序的实现方法
2017/07/14 Python
python使用turtle库绘制树
2018/06/25 Python
Python-ElasticSearch搜索查询的讲解
2019/02/25 Python
Python定时任务APScheduler的实例实例详解
2019/07/22 Python
python多线程与多进程及其区别详解
2019/08/08 Python
Python Print实现在输出中插入变量的例子
2019/12/25 Python
python 图像判断,清晰度(明暗),彩色与黑白实例
2020/06/04 Python
浅谈tensorflow使用张量时的一些注意点tf.concat,tf.reshape,tf.stack
2020/06/23 Python
Python调用高德API实现批量地址转经纬度并写入表格的功能
2021/01/12 Python
HTML5之HTML元素扩展(上)—新增加的元素及使用概述
2013/01/31 HTML / CSS
澳大利亚最受欢迎的女士度假服装:Kabana Shop
2020/10/10 全球购物
校园招聘策划书
2014/01/09 职场文书
公司委托书怎么写
2014/08/02 职场文书
2014幼儿教师个人工作总结
2014/12/03 职场文书
员工家属慰问信
2015/03/24 职场文书
公司地址变更通知
2015/04/25 职场文书
2015年测量员工作总结
2015/05/23 职场文书
2016寒假假期总结
2015/10/10 职场文书
Python爬取某拍短视频
2021/06/11 Python
Javascript中Microtask和Macrotask鲜为人知的知识点
2022/04/02 Javascript
搭建zabbix监控以及邮件报警的超级详细教学
2022/07/15 Servers