Top Questions

  • 5.11 Years

    • Category : Php
    • Location : Mohali
    • Posted On : Jul 2, 2016

    When you’re using a sophisticated design, CSS files can quickly become very long, and takes time to load. I have compiled interresting ways of compressing CSS files by using PHP.

    (1.)
    <link rel="stylesheet" type="text/css" media="screen" href="/style.css.php"/>
    Once you successfully rename your css files, edit it and add the following code at the beginning of the file:
    <?php if(extension_loaded('zlib')){ob_start('ob_gzhandler');} header("Content-type: text/css"); ?>
     

    2 Answers

    View More

    admin

    admin

    5.3 Years

    Answered:

    Jul 1, 2016

    Then, add the next line to the very bottom and save the file.
    <?php if(extension_loaded('zlib')){ob_end_flush();}?>
     

    admin

    admin

    5.3 Years

    Answered:

    Jul 1, 2016

    The Perishable Press methodq

    Basically, The Perishable Press method works as Paul Stamatiou’s method, by renaming your .css files to .css.php (or .php alone) and adding this short code snippet on the beggining of your CSS file:

    <?php 
       ob_start ("ob_gzhandler");
       header ("content-type: text/css; charset: UTF-8");
       header ("cache-control: must-revalidate");
       $offset = 60 * 60;
       $expire = "expires: " . gmdate ("D, d M Y H:i:s", time()   $offset) . " GMT";
       header ($expire);
    ?>

    Next other method to be use

    I just stumbled upon this code snippet by German developer Reinhold Weber some minutes ago. The least I can say is that I like it.
    <?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", "\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();
    ?>

Add Your Answer

When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.