Return to Snippet

Revision: 30419
at August 13, 2010 11:17 by math89


Initial Code
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", "\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();

Initial URL
http://www.phpsnippets.info/compress-css-files-using-php

Initial Description
Paste the code in a file named style.php. Don’t forget to include your css files . Once done, open your HTML document and import style.php as you’ll import a css stylesheet.

Initial Title
Compress CSS files using PHP

Initial Tags
css, php

Initial Language
PHP