note 71299 added to function.ob-start
Just for simplicity's sake (and because I had to rewrite it for use at http://www.VideoSift.com anyway), here's a very simplified, pre-PHP5 version. Just add one call to dump_css_cache() for each of your CSS files.
<?php
ob_start('ob_gzhandler');
header('Content-Type: text/css; charset: UTF-8');
header('Cache-Control: must-revalidate');
$expire_offset = 0; // set to a reaonable interval, say 3600 (1 hr)
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expire_offset) . ' GMT');
function css_compress($buffer) {
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);// remove comments
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' '), '', $buffer);// remove tabs, spaces, newlines, etc.
$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;
}
function dump_css_cache($filename) {
$cwd = getcwd() . DIRECTORY_SEPARATOR;
$stat = stat($filename);
$current_cache = $cwd . '.' . $filename . '.' . $stat['size'] . '-' . $stat['mtime'] . '.cache';
// the cache exists - just dump it
if (is_file($current_cache)) {
include($current_cache);
return;
}
// remove any old, lingering caches for this file
if ($dead_files = glob($cwd . '.' . $filename . '.*.cache', GLOB_NOESCAPE))
foreach ($dead_files as $dead_file)
unlink($dead_file);
if (!function_exists('file_put_contents')) {
function file_put_contents($filename, $contents) {
$handle = fopen($filename, 'w');
fwrite($handle, $contents);
fclose($handle);
}
}
$cache_contents = css_compress(file_get_contents($filename));
file_put_contents($current_cache, $cache_contents);
echo $cache_contents;
}
dump_css_cache('_general.css');
?>
----
Server IP: 64.71.164.2
Probable Submitter: 208.247.75.49
----
Manual Page -- http://www.php.net/manual/en/function.ob-start.php
Edit -- https://master.php.net/note/edit/71299
Del: integrated -- https://master.php.net/note/delete/71299/integrated
Del: useless -- https://master.php.net/note/delete/71299/useless
Del: bad code -- https://master.php.net/note/delete/71299/bad+code
Del: spam -- https://master.php.net/note/delete/71299/spam
Del: non-english -- https://master.php.net/note/delete/71299/non-english
Del: in docs -- https://master.php.net/note/delete/71299/in+docs
Del: other reasons-- https://master.php.net/note/delete/71299
Reject -- https://master.php.net/note/reject/71299
Search -- https://master.php.net/manage/user-notes.php
|