2010-10-03

[PHP] output buffering 筆記


<?php
function compress($buffer) {
return $buffer;
}

ob_start(); /*開啟輸出緩衝*/
// or
ob_start('ob_gzhandler'); /*開啟輸出緩衝,並使用 gZip 壓縮輸出。*/
ob_start('compress'); /*加入自訂處理函數*/


/*取得緩衝內容*/
$contents = ob_get_contents();

/*取得緩衝內容的長度*/
$length = ob_get_length();


/*送出緩衝內容*/
ob_flush();

/*結束緩衝,並送出內容*/
ob_end_flush();


/*清除緩衝內容*/
ob_clean();

/*結束緩衝,並清除內容*/
ob_end_clean();



用 PHP 優化 CSS file - 轉載自:PHP: ob_start - Manual

<?php
ob_start("ob_gzhandler");
ob_start("compress");
header("Content-type: text/css; charset: UTF-8");
header("Cache-Control: must-revalidate");
$off = 0; # Set to a reaonable value later, say 3600 (1 hr);
$exp = "Expires: " . gmdate("D, d M Y H:i:s", time() + $off) . " GMT";
header($exp);

function compress($buffer) {
// remove comments
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
// remove tabs, spaces, newlines, etc.
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer);

$buffer = str_replace('{ ', '{', $buffer); // remove unnecessary spaces.
$buffer = str_replace(' }', '}', $buffer);
$buffer = str_replace('; ', ';', $buffer);
$buffer = str_replace(', ', ',', $buffer);
$buffer = str_replace(' {', '{', $buffer);
$buffer = str_replace('} ', '}', $buffer);
$buffer = str_replace(': ', ':', $buffer);
$buffer = str_replace(' ,', ',', $buffer);
$buffer = str_replace(' ;', ';', $buffer);
return $buffer;
}

require_once('screen.css');
require_once('layout.css');
require_once('custom.php');
require_once('titles.css');
require_once('bus.css');

沒有留言:

張貼留言

你好!歡迎你在我的 Blog 上留下你寶貴的意見。