千家信息网

PHP中ob_get_contents(),ob_end_clean(),ob_start()怎么用

发表于:2025-11-16 作者:千家信息网编辑
千家信息网最后更新 2025年11月16日,小编给大家分享一下PHP中ob_get_contents(),ob_end_clean(),ob_start()怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文
千家信息网最后更新 2025年11月16日PHP中ob_get_contents(),ob_end_clean(),ob_start()怎么用

小编给大家分享一下PHP中ob_get_contents(),ob_end_clean(),ob_start()怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

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 '';
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 < 4.3.0, you can simply add the following to use the function anyway:

复制代码 代码如下:


if (!function_exists("ob_get_clean")) {
function ob_get_clean() {
$ob_contents = ob_get_contents();
ob_end_clean();
return $ob_contents;
}
}
?>

以上是"PHP中ob_get_contents(),ob_end_clean(),ob_start()怎么用"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

缓冲区 缓冲 内容 输出 代码 浏览器 篇文章 浏览 函数 变量 服务器 服务 这样的话 不怎么 例子 大部分 数据 时候 更多 知识 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 广电网络技术员干什么 岳阳思流互联网科技有限公司 数据库查询最大值与最小值的差 曙光服务器一般工作温度是多少 在服务器里怎么联机 什么软件自带免费数据库 我的世界青花服务器 数据库中对象所有者 汕头应用软件开发报价 软件开发对电脑配置的要求 核酸检测国家数据库能保存多久 瑞德服务器系统管理工具 网络安全法只在我国使用吗 在与与软件开发 服务器是否能设置启动盘 武汉虹信软件开发招 信息网络安全有哪些证书 佛山专业软件开发批发价 调度是引用数据库吗 华为终端 软件开发 数据库应用系统设计工程师证书 卫健委网络安全工作 青岛存储服务器联系方式 手机怎么自动切换服务器 重庆綦江生鲜类的软件开发 公司网络安全检查反馈 平度商城软件开发公司 目前最强cpu服务器 win10桌面软件开发语言 php能做服务器吗
0