Minify and combine CSS files


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

Minify and combine CSS files. Instructions in code.


Copy this code and paste it in your HTML
  1. /*
  2.  
  3.   USING THIS FILE
  4.  
  5.   1. Add the CSS files you want included to the $file array. */
  6.  
  7. $file[] = 'reset.css';
  8. $file[] = 'typography.css';
  9. $file[] = 'design.css';
  10.  
  11. /*
  12.   2. Save this file.
  13.  
  14.   3. Point your HTML files to this script, eg.
  15.   <link rel="stylesheet" href="/this-file.php" />
  16.  
  17.   4. Save the HTML file and refresh browser.
  18.  
  19. */
  20.  
  21. header('Content-type: text/css; Charset=utf-8');
  22.  
  23. function minifyCSS($f)
  24. {
  25. return preg_replace(array('/\/\*[\s\S]*?\*\//','/\s*([;:{},>+])\s*/',
  26. '/;}/','/\r?\n/'),array('','\1','}',''),file_get_contents($f));
  27. }
  28. foreach ($file as $name)
  29. {
  30. if (file_exists($name))
  31. {
  32. echo minifyCSS($name);
  33. }
  34. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.