Compress CSS files using PHP

0

In order to save your precious bandwidth, you should compress your css files. Doing so is not hard at all using this snippet.

header('Content-type: text/css');
ob_start("compress");
function compress($buffer) {
/* remove comments */
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
/* remove tabs, spaces, newlines, etc. */
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer);
return $buffer;
}

/* your css files */
include('master.css');
include('typography.css');
include('grid.css');
include('print.css');
include('handheld.css');

ob_end_flush();

Usage


Paste the code in a file named style.php. Don’t forget to include your css files (As seen on line 11 in the example). Once done, open your HTML document and import style.php as you’ll import a css stylesheet:


<link rel="stylesheet" href="style.php" type="text/css" media="all">

style.php will contain all your css stylesheets, compressed.

Labels:
Loading related posts...
Reactions: 

0 comments:

Post a Comment

2010 WEBSITE20. All rights reserved.