Merge CSS with PHP

I found this on a website,

css_loader.php

<?php // First of all send css header header("Content-type: text/css"); // Array of css files $css = array( 'main.css', 'menu.css', 'content.css' ); // Loop the css Array foreach ($css as $css_file) { // Load the content of the css file $css_content = file_get_contents($css_file); // print the css content echo $css_content; } ?> 

Add CSS to the page

 <link href="css_loader.php" rel="stylesheet" type="text/css" /> 

It looks very logical, but when I applied it to my page, it did not work.

Is it possible to combine CSS files this way?

+8
css php
source share
5 answers

You have an error in your code, the correct code is here:

 <?php // First of all send css header header("Content-type: text/css"); // Array of css files $css = array( 'main.css', 'menu.css', 'content.css' ); // Prevent a notice $css_content = ''; // Loop the css Array foreach ($css as $css_file) { // Load the content of the css file $css_content .= file_get_contents($css_file); } // print the css content echo $css_content; ?> 

And I hope the files are in the same folder. Perhaps you should use __DIR__ or dirname(__FILE__) to get the relative path to your files.

+10
source share

Stoney showed you his mistake .... now for a better version

  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('main.css'); include('menu.css'); include('content.css'); ob_end_flush(); 
+11
source share
 <?php // Array of css files $css = array( 'first.css', 'second.css' ); $mergeCSS = ""; // Loop the css Array foreach ($cssas $css_file) { // Load the content of the css file $mergeCSS.= file_get_contents($css_file); } // Remove comments also applicable in javascript $mergeCSS= preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $mergeCSS); // Remove space after colons $mergeCSS= str_replace(': ', ':', $mergeCSS); // Remove whitespace $mergeCSS= str_replace(array("\n", "\t", ' ', ' ', ' '), '', $mergeCSS); //Generate Etag $genEtag = md5_file($_SERVER['SCRIPT_FILENAME']); // call the browser that support gzip, deflate or none at all, if the browser doest support compression this function will automatically return to FALSE ob_start('ob_gzhandler'); // call the generated etag header("Etag: ".$genEtag); // Same as the cache-control and this is optional header("Pragma: public"); // Enable caching header("Cache-Control: public "); // Expire in one day header('Expires: ' . gmdate('D, d MYH:i:s', time() + 86400 ) . ' GMT'); // Set the correct MIME type, because Apache won't set it for us header("Content-type: text/javascript"); // Set accept-encoding header('Vary: Accept-Encoding'); // Write everything out echo($mergeCSS); ?> 

This code is also compatible for merging javascript

+1
source share

I wrote this class for such needs, it can display the output and compress it, later I can add the final result recording file, link here

+1
source share

Hey, you checked if your server is adding a few lines to your code (e.g. analytics code). if so, you should add a rule in .htaccess that stops the server to add lines to your code. this can be done by adding it to .htaccess - "php_value auto_append_file none" without quotes, of course. i hope this helps

0
source share

All Articles