Created
January 26, 2012 23:07
-
-
Save timw4mail/1685711 to your computer and use it in GitHub Desktop.
CSS Minification function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Function for compressing the CSS as tightly as possible | |
function compress($buffer) { | |
//Remove CSS comments | |
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); | |
//Remove tabs, spaces, newlines, etc. | |
$buffer = preg_replace('`\s+`', ' ', $buffer); | |
$replace = array( | |
' )' => ')', | |
') ' => ')', | |
' }' => '}', | |
'} ' => '}', | |
' {' => '{', | |
'{ ' => '{', | |
', ' => ',', | |
': ' => ':', | |
'; ' => ';', | |
); | |
//Eradicate every last space! | |
$buffer = trim(strtr($buffer, $replace)); | |
$buffer = str_replace('{ ', '{', $buffer); | |
$buffer = str_replace('} ', '}', $buffer); | |
return $buffer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment