Created
December 13, 2016 00:53
-
-
Save wpexplorer/9e3d99e556412e0b242d011e333c99a0 to your computer and use it in GitHub Desktop.
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
| 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