有时候就是得这么麻烦,为了减少部分东西,自己就弄了个PHP压缩CSS的,实际上除了一堆正则表达式和字符串替换,啥都没有。作用就是将一段展开的代码压缩一下,但和单行压缩又有不同,这个没有进行单行压缩,将其中的换行只保留了一个。
压缩后可以参考后面的CSS,如最终的效果,还是可以的,只是PHP的压缩代码还有较大的优化空间,下面的CSS通过格式化工具可以完整的还原为展开的代码,其实单行的也可以。
<?php
$content = file_get_contents($argv[1]);
$content = preg_replace("/[\\t]+/","",$content);
$content = preg_replace("/[ ]+/"," ",$content);
$content = str_replace(": ",":",$content);
$content = preg_replace("/;[\\r\\n]+/",";",$content);
$content = preg_replace("/{[\\r\\n]+/","{",$content);
$content = preg_replace("/;[\\s]+/",";",$content);
$content = preg_replace("/[\\r\\n]{2,}/","\r\n",$content);
$content = preg_replace("/,[\\s]+/",",",$content);
$content = preg_replace("/[\\s]+}/","}",$content);
$content = preg_replace("/;\\/\\*/",";\r\n/*",$content);
$content = preg_replace("/^[\\s]+/","",$content);
$content = preg_replace("/([\r\n]+)[\\s]+/","\$1",$content);
$content = preg_replace("/[\\s]+{[\\s]+/","{",$content);
$content = preg_replace("/[\\s]+}[\\s]+/","}",$content);
$content = str_replace([" }","{ "," {","} ","{/*"],["}","{","{","}","{\r\n/*"],$content);
$content = preg_replace("/([^;}])}/","\$1;}",$content);
$content=preg_replace("/\\){([^\r^\n])/","){\r\n\$1",$content);
$content = str_replace(['}}'],["}\r\n}"],$content);
echo $content;
$content = file_get_contents($argv[1]);
$content = preg_replace("/[\\t]+/","",$content);
$content = preg_replace("/[ ]+/"," ",$content);
$content = str_replace(": ",":",$content);
$content = preg_replace("/;[\\r\\n]+/",";",$content);
$content = preg_replace("/{[\\r\\n]+/","{",$content);
$content = preg_replace("/;[\\s]+/",";",$content);
$content = preg_replace("/[\\r\\n]{2,}/","\r\n",$content);
$content = preg_replace("/,[\\s]+/",",",$content);
$content = preg_replace("/[\\s]+}/","}",$content);
$content = preg_replace("/;\\/\\*/",";\r\n/*",$content);
$content = preg_replace("/^[\\s]+/","",$content);
$content = preg_replace("/([\r\n]+)[\\s]+/","\$1",$content);
$content = preg_replace("/[\\s]+{[\\s]+/","{",$content);
$content = preg_replace("/[\\s]+}[\\s]+/","}",$content);
$content = str_replace([" }","{ "," {","} ","{/*"],["}","{","{","}","{\r\n/*"],$content);
$content = preg_replace("/([^;}])}/","\$1;}",$content);
$content=preg_replace("/\\){([^\r^\n])/","){\r\n\$1",$content);
$content = str_replace(['}}'],["}\r\n}"],$content);
echo $content;
/*-----------------------------------------------------------------------------------*
/*Headings
/*-----------------------------------------------------------------------------------*/
h1,h2,h3,h4{font-family:Tahoma,Arial,"微软雅黑","宋体";color:#000000;line-height:1.5em;font-weight:normal;text-transform:capitalize;letter-spacing:1px;}
h1{font-size:16px;height:30px;line-height:1.2em;}
h2{font-size:21px;}
h3{font-size:16px;}
h4{font-size:14px;}
h2,h3{margin-top:30px;margin-bottom:20px;}
h4{margin-top:16px;margin-bottom:22px;}
h1 a,h2 a,h3 a,h4 a{color:#000000;}
#post-header{margin-top:0;margin-bottom:22px;font-size:14px;color:#999999;background-color:#fafafa;padding:7px 0 7px 10px;}
h5{font-size:12px;color:#999999;}
h5 a{font-size:12px;}
h5 span{color:#666666;}
img.wp-smiley-select{cursor:pointer;}
/*Headings
/*-----------------------------------------------------------------------------------*/
h1,h2,h3,h4{font-family:Tahoma,Arial,"微软雅黑","宋体";color:#000000;line-height:1.5em;font-weight:normal;text-transform:capitalize;letter-spacing:1px;}
h1{font-size:16px;height:30px;line-height:1.2em;}
h2{font-size:21px;}
h3{font-size:16px;}
h4{font-size:14px;}
h2,h3{margin-top:30px;margin-bottom:20px;}
h4{margin-top:16px;margin-bottom:22px;}
h1 a,h2 a,h3 a,h4 a{color:#000000;}
#post-header{margin-top:0;margin-bottom:22px;font-size:14px;color:#999999;background-color:#fafafa;padding:7px 0 7px 10px;}
h5{font-size:12px;color:#999999;}
h5 a{font-size:12px;}
h5 span{color:#666666;}
img.wp-smiley-select{cursor:pointer;}
正则看着就头疼,尤其是转义的正则
我对css不懂,一直想学的。
慢慢接触就可以了。自然而然就会了
不影响效果就行!
是的,没有必要刻意去追求这些东西
自己平常手写就是这个风格的怎么破 ……
那挺好的,天然压缩
手写就是这个风格啊……不忍直视
这样有什么好处
学习了
额额。欢迎再来
现在大多数CSS压缩都给整成了一行。
博主有想法啊,适当压缩,不但减小了体积也不影响代码查看。
怎么说呢。这是别人很早以前的想法,只是我一直未找到合适的工具
对于压缩,真心不熟,压缩对比没有压缩的,效果是否明显?
我的大概减少5k的内容,虽然无所谓。但是还是可以试试的
一般用了缓存或那些什么加速之类的,还用不用压缩呢
你这样理解吧,压缩代码之后,在数据量小时肯定不明显,但大量请求下优化还是蛮明显的
css本来生成的就乱,调试样式要挨个去看,当时学Html的时候就是这样
css本身是不乱的,但是初学者往往写的很混乱,所以就调试麻烦,还有自动生成的样式能用么?
那肯定的,学多了自然而然就熟悉了
我css学的一直都不好。。。
很显然这个关系不大!CSS得积累,毕竟东西很多
感觉源代码看起更加整洁一样,行外人的观点。
如果直接展开确实源代码比较整洁,现在需要的结果不一样
原来CSS样式还可以这样压缩。。
一切皆有可能嘛