Skip to content

Instantly share code, notes, and snippets.

@abbotto
Created October 26, 2012 16:04
Show Gist options
  • Save abbotto/3959645 to your computer and use it in GitHub Desktop.
Save abbotto/3959645 to your computer and use it in GitHub Desktop.
Compress Any Text-Based Content.
function crush($_file) {
$_file = preg_replace(array("/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/","!/\*[^*]*\*+([^/][^*]*\*+)*/!"), "", $_file); /* remove comments */
$_file = str_replace(array('\r\n','\r','\t','\n',' ',' ',' '), '', $_file); /* remove tabs, spaces, newlines, etc. */
/* remove spaces before/after certain characters */
$_file = preg_replace(array('(( )+\))','(\)( )+)'), ')', $_file); $_file = preg_replace(array('(( )+\()','(\(( )+)'), '(', $_file);
$_file = preg_replace(array('(( )+\})','(\}( )+)'), '}', $_file); $_file = preg_replace(array('(( )+\{)','(\{( )+)'), '{', $_file);
$_file = preg_replace(array('(( )+\:)','(\:( )+)'), ':', $_file); $_file = preg_replace(array('(( )+\;)','(\;( )+)'), ';', $_file);
$_file = preg_replace(array('(( )+\,)','(\,( )+)'), ',', $_file); return $_file;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment