Compress CSS files using PHP


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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.  

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.