Compress multiple CSS Files

If you are using multiple CSS files on a large website sometimes they can take along time to long the best way to deal with this is to compress the CSS by removing all comments and spaces in the file. Below is a PHP snippet which will combine mutliple files and removing all comments and spaces from the file, reducing the size of the file and therefore speeding up the load time.

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();

Advertise here 50% Off

Comment