【问题标题】:ob_start Caching CSSob_start 缓存 CSS
【发布时间】:2013-02-13 02:40:56
【问题描述】:

我正在使用这个 php 代码将 css 文件缓存到一个中间自己的 CMS 中。

<?php
ob_start("ob_gzhandler");
ob_start("compress");
header("Content-type: text/css; charset: UTF-8");
header("Cache-Control: must-revalidate");
$off = 3600;
$exp = "Expires: " . gmdate("D, d M Y H:i:s", time() + $off) . " GMT";
header($exp);

function compress($buffer) {
    $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); // remove comments
    $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer); // remove tabs, spaces, newlines, etc.
    return $buffer;
}

require_once('style1.css');
require_once('style2.css');

?>

此代码的最大限制是我无法将参数传递给我的“压缩”函数。 例如,如果我有一个 css 文件到另一个目录,图像的相对路径不会被替换。

你知道当我调用压缩函数并使用类似这样的东西时添加参数的方法吗?

$buffer = str_replace('url("', 'url("'.$directory, $buffer);

非常感谢任何建议!


编辑 -> 最终解决方案

在@Jack 提出建议后,我来到了这个来源。

用法:在html页面的header中加入这一行: &lt;link href="cacheCSS.php" rel="stylesheet" type="text/css" /&gt;

ob_start("ob_gzhandler");

class CWD {  
    private $path;

    public function __construct($path=NULL){
        if(!isset($path)) $path = '';
        $this->setPath($path);
    }

    public function setPath($path){
        $this->path = $path;
    }

    public function getPath() {
        return $this->path;
    }
}

$directory = new CWD();
$compress = function($buffer) use ($directory) {
    $buffer = str_replace('url("', 'url("'.$directory->getPath(), $buffer);
    $buffer = str_replace('url(\'', 'url(\''.$directory->getPath(), $buffer);
    $buffer = preg_replace('#^\s*//.+$#m', "", $buffer);
    $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
    $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
    return $buffer;
};

ob_start($compress);

header("Content-type: text/css; charset: UTF-8");
header("Cache-Control: must-revalidate");
$off = 0; # Set to a reaonable value later, say 3600 (1 hr);
$exp = "Expires: " . gmdate("D, d M Y H:i:s", time() + $off) . " GMT";
header($exp);

/* List of CSS files*/
$directory->setPath('path1/');
include('path1/style1.css');

ob_flush();
$directory->setPath('path2/');
include('path2/style2.css');

ob_flush();
$directory->setPath('path3/');
include('path3/style3.css');

【问题讨论】:

    标签: php css caching ob-start


    【解决方案1】:

    从 PHP 5.3 开始,您可以使用闭包来完成此操作:

    $directory = 'whatever';
    
    $compress = function($buffer) use ($directory) {
        $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); // remove comments
        $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer); // remove tabs, spaces, newlines, etc.
    
        $buffer = str_replace('url("', 'url("'.$directory, $buffer);
    
        return $buffer;
    }
    
    ob_start($compress);
    

    更新

    如果你的目录可以改变,你可能想使用一个对象来代替:

    class CWD
    {
        private $path;
    
        public function __construct($path)
        {
            $this->setPath($path);
        }
    
        public function setPath($path)
        {
            $this->path = $path;
        }
    
        public function getPath()
        {
            return $this->path;
        }
    }
    
    $directory = new CWD('/path/to/directory');
    
    $compress = function($buffer) use ($directory) {
        // ...
        $buffer = str_replace('url("', 'url("'.$directory->getPath(), $buffer);
    };
    

    然后在你的代码中的某个地方你会拥有:

    $directory->setPath();
    echo "whatever css";
    

    【讨论】:

    • 这可能是一个很好的提示,但路径目录不固定,取决于 require 指令。例如:require_once('dir1/style.css'); --> $directory = 'dir1/'; require_once('dir2/style.css'); --> $directory = 'dir2/';感谢您的耐心等待。
    • 如果每个文件的路径保持固定,您可以在包含上方指定它们。 $目录='dir1/'; require_once('dir1/style1.css');然后将“use($directory)”改为“use(&$directory)”,通过引用传入变量。
    • @Danilo 如果它改变了,你可以把它变成一个对象。
    • 编辑了问题。使用@Jack 解决方案,结果仍然是错误的.. :(
    • @danilo 您可能必须在更改目录之前尝试刷新缓冲区。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多