1

found a nice program in this discusson that combines CSS files into one, to speed up and fasten the CSS load. It works and makes all my css files into one tiny heap, BUT the page shows as text (the webpage becomes a css file, apache thinks) what can be wrong?

What i did: I saves the below script as a php file and included that into my page, and changed the urls to the css files with theirs. The css loads fine, but the WEBPAGE shows up as plain text into the browser! while all css is there and the rest of the php generated items too, only thing is the browser things it should load is as plain text, instead of html website... Any clues?

<?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();
?>

Furthermore improvements could be made later to even improve its strength, once working:

"; }" > "}" (2 chars)
"{ " > "{" (1 char)
" {" > "{" (1char)
" :" > ":" (1 char)
": " > ":" (1 char) " ," > "," (1 char)
", " > "," (1 char) " (" > "(" (1 char)
"( " > "(" (1 char) " )" > ")" (1 char)
") " > ")" (1 char)

1 Answer 1

3

How are you including that snippet?

At a guess, I'd imagine that should be it's own file and included via <link rel="stylesheet" type="text/css" href="./css.php" /> or similar. If you pasted that straight into index.php, you're setting the MIME type of it's output to text/css (via the header()), hence the plaintext.

2
  • ...also, just as a side-note: if you're familiar enough with PHP, you'd be doing yourself and your web server a huge favor by caching the compressed/minified CSS to a static file, so it doesn't have to parse all that stuff with every single page load. Client browsers will cache it just fine, but the CSS probably won't change often enough to churn through all that parsing on every single request. Alternately, manually minifying as needed will do the same with less server stress. Commented Dec 9, 2010 at 5:43
  • Interesting Tadamson! I included the script as a separate php file like so <? include (folder/csscombined.php) ; ?> into the homepage. What you are saying, is to fool the browser as if its a static css. Sounds even better! THAT file then indeed should be headered like css. I didnt came up with the simple logic behind this pfff. Thanks! Will change it and check back here tonight. Cheers! Commented Dec 9, 2010 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.