Posted By

math89 on 08/13/10


Tagged

css php compression


Versions (?)

Who likes this?

8 people have marked this snippet as a favorite

mgerdt
pytheas
ruinyourfun
mikael12
userpiv
booski
smonteru
voove


Compress CSS files using PHP


 / Published in: PHP
 

URL: http://www.phpsnippets.info/compress-css-files-using-php

Paste the code in a file named style.php. Don’t forget to include your css files . Once done, open your HTML document and import style.php as you’ll import a css stylesheet.

  1. header('Content-type: text/css');
  2. ob_start("compress");
  3. function compress($buffer) {
  4. /* remove comments */
  5. $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
  6. /* remove tabs, spaces, newlines, etc. */
  7. $buffer = str_replace(array("
  8. ", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer);
  9. return $buffer;
  10. }
  11.  
  12. /* your css files */
  13. include('master.css');
  14. include('typography.css');
  15. include('grid.css');
  16. include('print.css');
  17. include('handheld.css');
  18.  

Report this snippet  

You need to login to post a comment.