How to improve this code to combine/compress CSS using PHP [closed]


Tags: php,css

Problem :

This is a popular snippet to compress multiple CSS with PHP.

 <?php
      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", "\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();
?>

Looking at the code, I can see that it will only compress the files, but will not combine them. How do I tweak it to combine the files too?



Solution :

Have you tried the code? I'm pretty sure it combines it as well. It does the following: it sets a callback function to replace comments, tabs spaces and newlines. Then, it includes multiple CSS files (which all go through the same callback).

In the end, the output is flushed to the browser. That would mean all CSS of all those files will be sent to the browser, which means it combines it as well.


    CSS Howto..

    how can i add a css file to body of a page?

    How to make the float left and float right on the same line?

    How to use CSS to change icon color

    How to use the computer modern font in webpages?

    Showing :before but hiding element with CSS only

    How to not display css padding when span element is empty

    css - how to add another div above one with 2 colums layout?

    How do I align my navigation to the right without removing (display: inline;)?

    How to only change left padding in javafx css

    How to align TH Header with TD in TBody

    css responsive background image not showing

    How to apply css mark on videogular time line at specific time

    How to configure opacity based CSS overlay to not effect spacing of surrounding elements?

    How to select with css specific id only if have specific class on same div?

    How to turn on Visual Studio 2010 .css Intellisense on .less file

    How can I style a select option like a table?

    In CSS/JS, how to word-wrap at every nth word?

    How to change CSS element of data-pid using jquery

    How to increase height of footer's div when browser window is resized?

    How do I properly inspect jquery and javascript using web developer tools?

    How to Highlight a list item after user clicks on link

    how to get particular words with dotted lines in HTML and CSS?

    CSS: how to make a horizontal images scroller with two images per column?

    How to add vertical scrollbar to sliding tab? [closed]

    How to reject specify HTML tags by using css or xpath selector

    How do i add an external CSS file as well as CSS in the HTML file?

    How to indent text in a select drop down option menu using PHP & CSS? [REDUX]

    How to add a google map icon to this code?

    How to get rid of spacing between Bootstrap panels?

    How to rotate the text Rotation with center to the Number