Greetings everyone,
I have the following issue.
I’m trying to minimize my site’s loading time and requests and I thought that a css and js parser would be nice.
The js parser works fine (it just merges the files into a single one).
The css parser apart from merging some css files (all within a directory) it tries to cut the whitespaces to reduce its output size.
The code is as follows:
header('Content-type: text/css');
ob_start("compress_css");
function compress_css($buffer) {
/* remove comments */
$buffer = preg_replace('!/\\*[^*]*\\*+([^/][^*]*\\*+)*/!', '', $buffer);
/* remove tabs, spaces, newlines, etc. */
$buffer = str_replace(array("\\r\
", "\\r", "\
", "\ ", ' ', ' ', ' '), '', $buffer);
return $buffer;
}
/* your css files */
$files = scandir(".");
foreach ($files as $filename) {
if (!preg_match("/(.css)$/", $filename))
continue;
echo file_get_contents($filename);
}
ob_end_flush();
Mainly it’s a snippet I found on a site, apart from the foreach loop.
In the index.html file of my page (I use templates), I include the file as a regular css file.
<link type="text/css" rel="stylesheet" href="styles/css.php" />
<script type="text/javascript" src="libs/js.php"></script>
<!--js line nothing to do with css, just copying it -->
The file is being displayed correctly when displayed by itself (i.e. http://example.com/css.php). The output is OK and every file is included.
When I try to view the page with IE 8, it displays just fine.
When I try to view the page with Firefox 3.6.13 it displays the page but the css file is not linked, thus not used even though it gets downloaded. I get a page as if no stylesheet is present. (Inline styles are being displayed since they have nothing to do with the external files).
It’s been bugging me for quite some time now.
Any thoughts most welcome,
-john