2012-03-25 24 views
8

合併,我發現這個網站上,CSS與PHP

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; 
} 
?> 

添加CSS頁面

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

它看起來很合邏輯,但是當我把它應用到我的網頁,它沒有工作。

是否可以用這種方式合併CSS文件?

+0

當然這是可能的。爲什麼它不適合你?沒有錯誤?沒有? – Brad 2012-03-25 18:44:32

+1

什麼「不起作用」是什麼意思?當你直接從瀏覽器訪問'css_loader.php'並保存我們的成果時,你看到了什麼? – Yaniro 2012-03-25 18:45:08

+0

你是如何將它應用到你的頁面的?已經輸出到瀏覽器後,標題不起作用。另外CSS需要放在''部分而不是body中,所以它對你如何應用這一點有所不同。 – sdjuan 2012-03-25 18:46:11

回答

10

你在你的代碼中的錯誤,這裏是正確的代碼:

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

我希望這些文件是在同一文件夾。也許你應該使用__DIR__dirname(__FILE__)來獲取文件的相對路徑。

+0

耶固定了這個。但仍CSS不適用於頁面。 – 2012-03-25 18:52:41

+0

我測試過這種情況。也許你的php文件有錯誤。調用文件direkt並測試它。當其中一個文件丟失的時候,你會得到Warning:file_get_contents(main.css)和瀏覽器無法處理的文本/ css流。 – Stony 2012-03-25 19:00:43

11

斯托尼向您展示了您的錯誤....現在更改進型

 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(); 
+0

不錯,謝謝 – 2012-03-25 22:30:34

+0

歡迎您:) – Baba 2012-03-25 22:36:21

0

嘿有,如果你的服務器追加幾行代碼(例如Analytics(分析)代碼)你檢查。如果是這樣,您必須在.htaccess中添加一條規則,以停止服務器向您的代碼添加行。這可以通過將這添加到.htaccess來完成 - 「php_value auto_append_file none」當然沒有引號。我希望這有助於

1
<?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 M Y H: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); 

?> 

此代碼還兼容用於合併JavaScript的

+1

將內容類型從text/javascript更改爲text/css – dai 2016-09-07 15:55:42

1

我寫了這個類這樣的需求,它可以使輸出和壓縮它,以後我可以添加寫出最終結果的文件,link here