首先将所有 css 放到一个目录里,然后在此目录新建一个空的 css 文件,命名为 css.php(其实除了后缀命名随便)。
然后在 php 文件里放下边的代码:
代码如下 复制代码
header('content-type: text/css');
ob_start(compress);
function compress($buffer) {
$buffer = preg_replace('!/*[^*]**+([^/][^*]**+)*/!', '', $buffer);
$buffer = str_replace(array(rn, r, n, t, ' ', ' ', ' '), '', $buffer);
return $buffer;
}
/*include('colorpicker.css');
include('jquery-ui.css');
include('style.css');
include('switchery.min.css');*/
foreach( glob( *.css ) as $filename ) include $filename;
ob_end_flush();
引入 css 文件的代码换成引入这个 php 文件,例如:
压缩多个css为一个css
代码如下 复制代码
pack();
$js_content .= $append_content;
}
@file_put_contents($js_url,$js_content);
}
return $js_url;
}
前台
js调用
css调用
php 文件里就包含了所有被压缩的 css 代码,而且可以自动引入 css 目录里的所有 css 文件,不用在新建 css 文件的时候再修改这个 php 文件。
