如何使用ob函数输出静态html文件

本文介绍PHP中ob函数的使用方法,包括开启、获取、清除缓冲区等操作,并展示了如何利用这些函数生成静态HTML文件,特别是从数据库中获取信息生成页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何使用ob函数输出静态html文件

1、ob函数介绍

1.1、ob_start — 打开输出控制缓冲
bool ob_start ([ callback $output_callback [, int $chunk_size [, bool $erase ]]] )  
此函数将打开输出缓冲。当输出缓冲激活后,脚本将不会输出内容(除http标头外),相反需要输出的内容被存储在内部缓冲区中。
详情参考:http://php.net/manual/zh/function.ob-start.php
1.2、ob_get_contents — 返回输出缓冲区的内容
string ob_get_contents ( void )
只是得到输出缓冲区的内容,但不清除它。
详情参考:http://php.net/manual/zh/function.ob-get-contents.php
1.3、ob_end_flush — 冲刷出(送出)输出缓冲区内容并关闭缓冲
bool ob_end_flush ( void )
这个函数将送出最顶层缓冲区的内容(如果里边有内容的话),并关闭缓冲区。如果想进一步处理缓冲区中的内容,必须在ob_end_flush()之前调用 ob_get_contents(),因为在调用ob_end_flush()后缓冲区内容被丢弃。
详情参考:http://php.net/manual/zh/function.ob-end-flush.php
1.4、ob_flush — 冲刷出(送出)输出缓冲区中的内容
void ob_flush ( void )
这个函数将送出缓冲区的内容(如果里边有内容的话)。如果想进一步处理缓冲区中的内容,必须在ob_flush()之前调用ob_get_contents() ,因为在调用ob_flush()之后缓冲区内容将被丢弃。
此函数不会销毁输出缓冲区,而像ob_end_flush() 函数会销毁缓冲区。
详情参考:http://php.net/manual/zh/function.ob-flush.php
1.5、ob_get_clean — 得到当前缓冲区的内容并删除当前输出缓
string ob_get_clean ( void )
得到当前缓冲区的内容并删除当前输出缓冲区。
ob_get_clean() 实质上是一起执行了 ob_get_contents() 和 ob_end_clean()。
详情参考:http://php.net/manual/zh/function.ob-get-clean.php
1.6、ob_get_flush — 刷出(送出)缓冲区内容,以字符串形式返回内容,并关闭输出缓冲区
string ob_get_flush ( void )
ob_get_flush() 刷出(送出)缓冲区内容,以字符串形式返回内容,并关闭输出缓冲区。
Note: 这个函数与ob_end_flush()相似,不同的是本函数还会以字符串形式返回缓冲区内容。
详情参考:http://php.net/manual/zh/function.ob-get-flush.php

2、如何使用ob()函数来制作html的静态页面

2.1、简单输出html文件
<?php
ob_start(); //打开缓冲区
$info = 'hello world!!';
$file=fopen('index.html','w'); //打开文件index.html
fwrite($file,$info); //写入信息到index.html
fclose($file); //关闭文件index.html
?>
输出hello到index.html

找到index.html,正常输出了设定的内容
2.2、获取数据库信息输出html文件
<?php
require_once 'coon.php';
$sql = "select * from name order by id;";
$result = $link->query($sql);
$arr = array();
while($re = $result->fetch(PDO::FETCH_ASSOC)){ 
$arr[] = $re;

//循环输出内容到html文件
ob_start(); //打开缓冲区 
?>
<!-- 下面是输出的内容 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>循环输出的html内容</title>
</head>
<body>
<table>
<thead>
<tr>
<td>id</td>
<td>name</td>
<td>pwd</td>
</tr>
</thead>
<tbody>
<?php
foreach ($arr as $key => $value) {
echo "<tr>"; 
echo "<td>{$value['id']}</td>";
echo "<td>{$value['name']}</td>";
echo "<td>{$value['pwd']}</td>"; 
echo "</tr>";
}
?>
</tbody>
</table> 
</body>
</html>
<?php
$content = ob_get_contents();//得到当前缓冲区的内容
ob_end_clean();//删除当前输出缓
file_put_contents('index2.html',$content);//写入文件
?>
输出结果到index2.html

 

 

Output Control 函数有很多,大致就先介绍这几种

 

2.3 优化读取方式,确定正确读取指定文件
<?php
	$fileName = 'index2.html'; $re = file_exists($fileName);//判断文件是否存在 $dValue = 0; if($re){ $fileTime = filectime($fileName);//时间戳 $dValue = time() - $fileTime;//获取创建时间,文件缓存一般存在有效期 } if(file_exists($fileName) && $dValue < 3600){ $content = file_get_contents($fileName); echo $content; die; }else{ if($re){ unlink($fileName);//过去先删除, } require_once 'coon.php'; $sql = "select * from name order by id;"; $result = $link->query($sql); $arr = array(); while($re = $result->fetch(PDO::FETCH_ASSOC)){ $arr[] = $re; } //循环输出内容到html文件 ob_start(); //打开缓冲区  ?> <!-- 下面是输出的内容 --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>循环输出的html内容</title> </head> <body> <table> <thead> <tr> <td>id</td> <td>name</td> <td>pwd</td> </tr> </thead> <tbody> <?php foreach ($arr as $key => $value) { echo "<tr>"; echo "<td>{$value['id']}</td>"; echo "<td>{$value['name']}</td>"; echo "<td>{$value['pwd']}</td>"; echo "</tr>"; } ?> </tbody> </table> </body> </html> <?php $content = ob_get_contents();//得到当前缓冲区的内容 ob_end_clean();//删除当前输出缓 file_put_contents('index2.html',$content);//写入文件 

转载于:https://www.cnblogs.com/ImCehnyx/p/7147931.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值