【问题标题】:CSS merging with PHPCSS与PHP合并
【发布时间】:2012-04-09 09:53:38
【问题描述】:

我在一个网站上找到了这个,

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 文件吗?

【问题讨论】:

  • 当然可以。为什么它对你不起作用?没有错误?什么都没有?
  • “不起作用”是什么意思?当您直接从浏览器访问css_loader.php 并保存ourcome 时,您看到了什么?
  • 你是如何将它应用到你的页面的?在您已经输出到浏览器后,标题不起作用。此外,CSS 需要放在 &lt;head&gt; 部分而不是正文中,因此它对您应用它的方式有很大影响。
  • 我可以看到所有 CSS 内容,但我的页面看起来好像没有 CSS。没有错误:S
  • 404 处理程序即时资源构建:我通过实时图像调整大小(例如缩略图)来做到这一点。想法是,您将页面/应用程序设置为在静态 URL 处调用生成的资源(例如组合 CSS 或较小的图像),然后将服务器的 404 错误文档配置为脚本,该脚本基于请求的 URL 构建请求的文件,将其保存在请求的位置,将响应代码设置为 200,然后将其返回给浏览器。对同一资源的下一个请求将找到生成的文件并在没有 404 事件的情况下返回它。重新部署应该会删除过期文件。

标签: php css


【解决方案1】:

您的代码有误,这是正确的代码:

<?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__) 来获取文件的相对路径。

【讨论】:

  • 是的,解决了这个问题。但 CSS 仍然不适用于页面。
  • 我已经测试过这个案例。也许您的 php 文件中有错误。调用文件 direkt 并对其进行测试。当其中一个文件丢失时,您会收到警告:file_get_contents(main.css) 并进入文本/css 流,浏览器无法处理它。
【解决方案2】:

Stoney 已向您展示了您的错误 .... 现在提供更改进的版本

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

【讨论】:

    【解决方案3】:
    <?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

    【讨论】:

    • 将内容类型从 text/javascript 更改为 text/css
    【解决方案4】:

    我写这个类就是为了这样的需求,它可以渲染输出并压缩它,稍后我可以添加写入最终结果的文件,link here

    【讨论】:

      【解决方案5】:

      您是否检查过您的服务器是否在您的代码中附加了几行代码(例如分析代码)。如果是这样,您必须在 .htaccess 中添加一条规则,以阻止服务器向您的代码添加行。这可以通过将其添加到 .htaccess - "php_value auto_append_file none" 来完成,当然不带引号。我希望这会有所帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-18
        • 1970-01-01
        • 2015-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多