博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
css压缩_使用PHP轻松实现CSS压缩
阅读量:2519 次
发布时间:2019-05-11

本文共 2698 字,大约阅读时间需要 8 分钟。

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压缩

转载地址:http://kjpwd.baihongyu.com/

你可能感兴趣的文章
十二种获取Spring的上下文环境ApplicationContext的方法
查看>>
UVA 11346 Probability 概率 (连续概率)
查看>>
linux uniq 命令
查看>>
Openssl rand命令
查看>>
HDU2825 Wireless Password 【AC自动机】【状压DP】
查看>>
BZOJ1015: [JSOI2008]星球大战starwar【并查集】【傻逼题】
查看>>
HUT-XXXX Strange display 容斥定理,线性规划
查看>>
mac修改用户名
查看>>
一道关于员工与部门查询的SQL笔试题
查看>>
Canvas基础
查看>>
[Hive - LanguageManual] Alter Table/Partition/Column
查看>>
可持久化数组
查看>>
去除IDEA报黄色/灰色的重复代码的下划波浪线
查看>>
Linux发送qq、网易邮件服务配置
查看>>
几道面试题
查看>>
【转】使用 WebGL 进行 3D 开发,第 1 部分: WebGL 简介
查看>>
js用正则表达式控制价格输入
查看>>
chromium浏览器开发系列第三篇:chromium源码目录结构
查看>>
java开发操作系统内核:由实模式进入保护模式之32位寻址
查看>>
第五讲:单例模式
查看>>