Skip to content

Instantly share code, notes, and snippets.

@melat0nin
Created February 6, 2012 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melat0nin/1751745 to your computer and use it in GitHub Desktop.
Save melat0nin/1751745 to your computer and use it in GitHub Desktop.
PHP: Minify CSS
// from here: http://www.concrete5.org/documentation/how-tos/developers/dynamically-concatenate-minify-gzip-and-server-side-cache-multip/
function minifyCSS($css) {
$css = trim($css);
$css = str_replace("\r\n", "\n", $css);
$search = array("/\/\*[^!][\d\D]*?\*\/|\t+/", "/\s+/", "/\}\s+/");
$replace = array(null, " ", "}\n");
$css = preg_replace($search, $replace, $css);
$search = array("/;[\s+]/", "/[\s+];/", "/\s+\{\\s+/", "/\\:\s+\\#/", "/,\s+/i", "/\\:\s+\\\'/i", "/\\:\s+([0-9]+|[A-F]+)/i", "/\{\\s+/", "/;}/");
$replace = array(";", ";", "{", ":#", ",", ":\'", ":$1", "{", "}");
$css = preg_replace($search, $replace, $css);
$css = str_replace("\n", null, $css);
return $css;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment