css压缩
Loading time of a website is as important as its functionality. You can have a great website but who wants to wait for it to load? CSS compression can help your website load faster and easily maintain its functionality. I've created an easy-to-use PHP file to compress your CSS for optimal client download time.
网站的加载时间与其功能一样重要。 您可以拥有一个很棒的网站,但是谁愿意等待它加载? CSS压缩可以帮助您更快地加载网站并轻松维护其功能。 我创建了一个易于使用PHP文件来压缩CSS,以获得最佳的客户端下载时间。
代码 (The Code)
The process takes place in two files: one PHP file which we call css.css (yes, use the ".css" extension) and your directory's .htaccess file.
该过程分为两个文件:一个PHP文件(我们称为css.css (是,使用“ .css”扩展名))和目录的.htaccess文件。
Here's the PHP for css.css:
这是css.cssPHP:
$css = '';$root = $_SERVER['DOCUMENT_ROOT'].'/css/'; //directory where the css lives$files = explode(',',$_SERVER['QUERY_STRING']);if(sizeof($files)){ foreach($files as $file) { $css.= (is_file($root.$file.'.css') ? file_get_contents($root.$file.'.css') : ''); }}return str_replace('; ',';',str_replace(' }','}',str_replace('{ ','{',str_replace(array("\r\n","\r","\n","\t",' ',' ',' '),"",preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!','',$css)))));
The css.css file takes the given querystring and explodes it on a comma into an array. If there is something in the querystring (which there should be, unless the programmer messes up), which should be separated by a comma, the querystring gets separated into an array. For each string within the array, the script looks for a CSS file with a matching name. The script then appends the content of the file to a string variable ($css). Once all of the CSS content is within the $css variable, the CSS content is scrubbed of all whitespace. Less whitespace and one file means a much faster download speed since there is only one request for a compressed file.
css.css文件采用给定的查询字符串,并以逗号将其分解为数组。 如果查询字符串中有东西(除非程序员弄乱了,否则应该有),并且应该用逗号分隔,查询字符串会被分隔成一个数组。 对于数组中的每个字符串,脚本将查找具有匹配名称CSS文件。 然后,脚本将文件的内容附加到字符串变量( $ css )。 一旦所有CSS内容都在$ css变量内,就清除所有空白CSS内容。 更少的空格和一个文件意味着更快的下载速度,因为只有一个请求可压缩文件。
Here's the code you'll place into the .htaccess file so that css.css gets treated as a PHP file:
这是您将放入.htaccess文件中的代码,以便将css.css视为PHP文件:
SetHandler application/x-httpd-php
用法 (The Usage)
Using the css.css file is easy:
使用css.css文件很容易:
Essentially, in the example above, we're looking to use reset.css, base.css, forms.css, and template.css. If one of those files doesn't exist, they wont be added (and there's no error message).
从本质上讲,在上面的例子中,我们正在寻找使用reset.css,base.css,forms.css和template.css。 如果这些文件之一不存在,则不会添加它们(并且不会出现错误消息)。
Do you have any suggestion for my script? Any ideas to contribute that would optimize the CSS?
您对我的剧本有什么建议吗? 有什么想法可以优化CSS?
翻译自:
css压缩