Skip to content

Instantly share code, notes, and snippets.

@wpexplorer
Created December 13, 2016 00:53
Show Gist options
  • Select an option

  • Save wpexplorer/9e3d99e556412e0b242d011e333c99a0 to your computer and use it in GitHub Desktop.

Select an option

Save wpexplorer/9e3d99e556412e0b242d011e333c99a0 to your computer and use it in GitHub Desktop.
function wpex_minify_css( $css ) {
// Normalize whitespace
$css = preg_replace( '/\s+/', ' ', $css );
// Remove ; before }
$css = preg_replace( '/;(?=\s*})/', '', $css );
// Remove space after , : ; { } */ >
$css = preg_replace( '/(,|:|;|\{|}|\*\/|>) /', '$1', $css );
// Remove space before , ; { }
$css = preg_replace( '/ (,|;|\{|})/', '$1', $css );
// Strips leading 0 on decimal values (converts 0.5px into .5px)
$css = preg_replace( '/(:| )0\.([0-9]+)(%|em|ex|px|in|cm|mm|pt|pc)/i', '${1}.${2}${3}', $css );
// Strips units if value is 0 (converts 0px to 0)
$css = preg_replace( '/(:| )(\.?)0(%|em|ex|px|in|cm|mm|pt|pc)/i', '${1}0', $css );
// Return minified CSS
return trim( $css );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment