PHP关于ob_系列函数的学习总结

来源:互联网 发布:网络语 安利 编辑:程序博客网 时间:2024/05/20 23:03

PHP关于ob_系列函数的学习总结

最近在学习PHP关于报表与打印技术时,发现将页面内容保存到Word或Excel会经常用到ob系列函数,好奇之下在网上找了些资料系统的学习了一下,发现ob系列函数功能真的强大,各种小功能都可以利用ob系列函数实现。

ob系列函数作用

  • 写入缓存,控制输出;
  • 配合文件函数进行文件保存等操作;
  • ob系列函数可用于header()之前;

常用函数

  1. ob_start();打开输出缓冲区;
  2. ob_clean();删除缓冲区内容,不输出;
  3. ob_end_clean(); 删除缓冲区内容并关闭缓冲区,不输出;
  4. ob_get_clean();返回缓存内容的值并关闭缓冲区;
  5. ob_flush();输出缓冲区内容并删除;
  6. ob_end_flush();输出缓冲区内容并删除,关闭缓冲区;
  7. ob_get_flush();返回缓冲区内容并删除,关闭缓冲区;
  8. flush();将缓冲区所有内容输出并刷新缓冲区;
  9. ob_get_contents();返回缓冲区内容,不输出;
  10. ob_get_length();返回内容长度。如缓冲区未激活则返回False;
  11. ob_get_level();返回当前嵌套层次;
  12. ob_get_status();返回缓冲区状态;
  13. ob_implicit();打开或关闭绝对刷新(绝对刷新:缓冲区中的内容直接输出到页面,不再等待flush()等输出函数输出);
  14. ob_gzander();用gzip压缩缓冲区,ob_start()的回调函数;

使用实例(结合文件操作函数将表格保存入Word)

word.php

<?php    class word    {         function start()        {            ob_start();        }        function save($path)        {            $data = ob_get_contents();            ob_end_clean();            $this->wirteToWord($path,$data);        }        function wirteToWord ($fn,$data)        {            $fp=fopen($fn,"wb");            fwrite($fp,$data);            fclose($fp);        }    }?>

ob.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title>将表格存入word</title></head>    <body>    <?php    if($_GET[id]!="")     {       include_once("word.php");       $word=new word();       $word->start();     }    ?>    <table width="600" height="50" border="0" align="center" cellpadding="0" cellspacing="0">      <tr>        <td height="50" bgcolor="#B2CA86"><table width="600" height="50" border="0" align="center" cellpadding="0" cellspacing="1">            <tr>              <td width="127" height="25" bgcolor="#66ccff"><div align="center">A</div></td>              <td width="125" bgcolor="#66ccff"><div align="center">B</div></td>              <td width="104" bgcolor="#66ccff"><div align="center">C</div></td>              <td width="116" bgcolor="#66ccff"><div align="center">D</div></td>              <td width="122" bgcolor="#66ccff"><div align="center">E</div></td>            </tr>            <tr>              <td height="25" bgcolor="#FFFFFF"><div align="center">A</div></td>              <td height="25" bgcolor="#FFFFFF"><div align="center">B</div></td>              <td height="25" bgcolor="#FFFFFF"><div align="center">C</div></td>              <td height="25" bgcolor="#FFFFFF"><div align="center">D</div></td>              <td height="25" bgcolor="#FFFFFF"><div align="center">E</div></td>            </tr>            <?php             if($_GET[id]!="")              {                 $word->save("data.doc");//将表格保存到Word              }              if($_GET[id]=="")              {            ?>            <tr>              <td height="25" colspan="5" bgcolor="#FFFFFF"><div align="center">                  <input type="button" name="submit" value="保存到Word" class="buttoncss" onclick="window.location.href='ob.php?id=print'">                </div></td>            </tr>            <?php              }else{              echo "<div align=center>保存表格成功!</div>";              }           ?>          </table></td>      </tr>    </table>    </body></html>
原创粉丝点击