2

我正在使用这个 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页面的页眉中添加这一行: <link href="cacheCSS.php" rel="stylesheet" type="text/css" />

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

1 回答 1

3

从 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";
于 2013-02-13T02:53:20.590 回答