PHP經驗——輸出緩沖區處理函數:ob_start、ob_get_contents、ob_end_clean、ob_end_flush


輸出緩沖區處理函數的作用有點類似C的堆棧,臨時放入一個存儲空間中,對內容進行處理。

輸出緩沖區函數有:

  • ob_start() - 打開輸出控制緩沖
  • ob_get_length() - 返回輸出緩沖區的長度
  • ob_get_level() - 返回輸出緩沖區的嵌套級別
  • ob_get_status() - 返回輸出緩沖區的狀態(數組形式返回,默認返回最頂層,參數為true時返回所有)
  • ob_get_contents() - 返回輸出緩沖區的內容
  • ob_get_clean() - 以字符串格式返回當前輸出緩沖區並關閉輸出緩沖
  • ob_end_clean() - 清空(擦除)緩沖區並關閉輸出緩沖
  • ob_get_flush() - 以字符串返回輸出緩沖區內容並關閉緩沖
  • ob_end_flush() - 沖刷出(送出)輸出緩沖區內容緩沖

實例如下:

<?php 
 
 define('APP_ROOT', dirname(__FILE__));
 $file = '/templates/html/error_config.html';
 ob_start(); 
 include(APP_ROOT.$file);
 ob_end_flush();
   //$contents = ob_get_contents(); 這樣可以將輸出保存,可以作進一步處理
  //ob_end_clean();
   //echo $contents;
 ?>
<?php echo"<?xml version=\"1.0\" encoding=\"utf-8\">"; ?>
 <!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>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
     <meta http-equiv="Content-Script-Type" content="text/javascript" />
     <meta http-equiv="Content-Style-Type" content="text/css" />
     <title>Error</title>
 </head>
 <body onload="doRedirect()">
 <h1>Error</h1>
 <div style="position:absolute;top:150px;text-align:center;width:95%;">
     <p style="margin:12pt;"><strong>config.php</strong> does not exist or is not readable by the webserver in the directory.</p>
     <p style="margin:12pt;"><?php echo CommonFunctions::$PSI_VERSION_STRING ?></p>
 </div>
 </body>
 </html>
 

輸出結果如下:

config.php does not exist or is not readable by the webserver in the phpsysinfo directory.

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM