2012-03-25 17 views
8

Znalazłem to na stronie internetowej,scalanie 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; 
} 
?> 

Dodaj CSS do strony

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

Wygląda to bardzo logiczne, ale gdy zastosowano ją do moja strona, to nie działa.

czy można scalić w ten sposób pliki CSS?

+0

Oczywiście jest to możliwe. Dlaczego to nie działa? Bez błędów? Nic? – Brad

+1

Co oznacza "nie działa"? kiedy uzyskujesz dostęp do 'css_loader.php' bezpośrednio z przeglądarki i zapisujesz rezultat, co widzisz? – Yaniro

+0

W jaki sposób zastosowałeś go do swojej strony? Nagłówki nie działają po wyprowadzeniu do przeglądarki. Również CSS należy umieścić w sekcji '', a nie w treści, więc ma to wpływ na to, jak to zastosowałeś. – sdjuan

Odpowiedz

10

Masz błąd w kodzie, oto prawidłowy kod:

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

I mam nadzieję, że pliki są w tym samym folderze. Być może powinieneś użyć __DIR__ lub dirname(__FILE__), aby uzyskać względną ścieżkę do twoich plików.

+0

Tak, naprawiłem to. ale nadal CSS nie stosuje się do strony. –

+0

Przetestowałem tę sprawę. Być może masz błąd w swoim pliku php. zadzwoń do pliku direkt i przetestuj go. Gdy brakuje jednego z plików otrzymasz Ostrzeżenie: file_get_contents (main.css) i do strumienia tekst/css, którego przeglądarka nie może obsłużyć. – Stony

11

Stoney pokazało Ci swój błąd .... teraz bardziej ulepszonej wersji

 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

miło dziękuję –

+0

zapraszam :) – Baba

0

hej masz sprawdzić, czy Twój serwer dołącza kilka wierszy do kodu (na przykład kod Analytics). jeśli tak, to musisz dodać regułę do pliku .htaccess, który zatrzymuje serwer w celu dodania linii do twojego kodu. można to zrobić dodając to do .htaccess - "php_value auto_append_file none" bez cytatów oczywiście. Mam nadzieję, że to pomaga

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

?> 

ten kod również kompatybilny do łączenia JavaScript

+1

Zmiana typu zawartości z tekstu/javascript na text/css – dai

1

Napisałem tę klasę dla takich potrzeb, może to spowodować, że wyjście i ścisnąć ją, później mogę dodać zapisać plik z wynikami końcowymi , link here