» Snippets » php » Compress multiple CSS files

Compress multiple CSS files

  • Web site optimization geeks suggest that we should try to minimize the size of files while serving web pages. Most of the time, web designers use multiple CSS files for make task management easier, but this requires as many HTTP requests as there are CSS files. Following script will allow us to serve all your CSS files as a single HTTP resource, minified (by removing comments and extraneous whitespace), and gzip-compressed.

    header('Content-type: text/css');
    ob_start("compress");
    function compress($buffer) {
      /* remove comments */
      $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
      /* remove tabs, spaces, newlines, etc. */
      $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
      return $buffer;
    }
    
    /* your css files */
    include('master.css');
    include('typography.css');
    include('grid.css');
    include('print.css');
    include('handheld.css');
    
    ob_end_flush();

    Enjoy!

Publish your snippets with us & be an exclusive author!

  • Total View: 500 time(s)
  • Script Upload Date: 2012-07-01

Recommended Scripts More Recommended Snippets: