在链接元样式表中连接css和php


Concatenate css with php in link meta stylesheet

如何用php将多个css文件连接成一行?

我想减少以下内容…

<link href="reset.css" rel="stylesheet" type="text/css" media="screen">
<link href="grid.css" rel="stylesheet" type="text/css" media="screen">
<link href="commons.css" rel="stylesheet" type="text/css" media="screen">

<link href="concatenator.php+reset.css+grid.css+commons.css" rel="stylesheet" type="text/css" media="screen">

结果一个CSS或HTML文件与所有样式表在

该方法减少了http请求,并为设计人员和开发人员使用dinamyc站点配置提供了其他功能。

MatRt发布的代码很接近,但使用+号而不是。它似乎并没有真正将CSS发送到页面,尽管它会打印CSS。

我设法得到这里提供的代码工作与一个小的调整。

<?php
// Check the parameter and stop if there is no files param
if (empty($_GET['files']))
    die();
// Retrieve the list of files
$files = explode(",", $_GET['files']);
$directoryOfCss = 'C:'xampplite'htdocs'path'to'my'folder'css''';
$cssContent = "";
// Loop on all potential file and try to retrieve its content
foreach($files as $oneFile)
{
	//echo $directoryOfCss . $oneFile . ".css";
    if (file_exists($directoryOfCss . $oneFile . ".css")){
    	//echo $directoryOfCss . $oneFile . ".css<br>";
        $cssContent .= file_get_contents($directoryOfCss . $oneFile . ".css");
    }
}
// Finally echo the total content of CSS
header("Content-Type: text/css"); 
header("X-Content-Type-Options: nosniff");
header("Content-Type: text/css;X-Content-Type-Options: nosniff;");
echo $cssContent;

我从这篇文章中得到了标题部分

你的URL应该更像

concatenator.php?files=reset,grid,commons

注意:如果您不喜欢,

,您可以选择其他分隔符

著名的concatenator.php可能是

<?php
// Check the parameter and stop if there is no files param
if (empty($_GET['files']))
    die();
// Retrieve the list of files
$files = explode(",", $_GET['files']);
$directoryOfCss = "/absolute/path/to/your/css/files/";
$cssContent = "";
// Loop on all potential file and try to retrieve its content
foreach($files as $oneFile)
{
    if (file_exists($directoryOfCss + $oneFile + ".css"))
        $cssContent += file_get_contents($directoryOfCss + $oneFile + ".css");
}
// Finally echo the total content of CSS
echo $cssContent;