Home  >  Article  >  php教程  >  PHP:压缩CSS代码

PHP:压缩CSS代码

WBOY
WBOYOriginal
2016-06-06 20:09:18908browse

作为站长或者是开发者,应该都了解到CSS是决定页面视觉效果的一个魔法文件。而为了让页面效果更给力,我们往往需要写很多的CSS代码。CSS一多,网页加载的速度就会变慢下来。所以呢,减小CSS文件的大小就是一个必须的事情了。而压缩就是最常见的一种给CSS文件

作为站长或者是开发者,应该都了解到CSS是决定页面视觉效果的一个魔法文件。而为了让页面效果更给力,我们往往需要写很多的CSS代码。CSS一多,网页加载的速度就会变慢下来。所以呢,减小CSS文件的大小就是一个必须的事情了。而压缩就是最常见的一种给CSS文件减肥的方法。下面,我们就来看看如何通过PHP给CSS代码减肥:

<?php header(&#8216;Content-type: text/css&#8217;);   
ob_start(&#8220;compress&#8221;);   
function compress($buffer) {   
$buffer = preg_replace(&#8216;!/\*[^*]*\*+([^/][^*]*\*+)*/!&#8217;, &#8221;, $buffer);   
$buffer = str_replace(array(&#8220;\r\n&#8221;, &#8220;\r&#8221;, &#8220;\n&#8221;, &#8220;\t&#8221;, &#8217; &#8217;, &#8217; &#8217;, &#8217; &#8217;), &#8221;, $buffer);   
return $buffer;   
}   
include(&#8216;master.css&#8217;);   
include(&#8216;typography.css&#8217;);   
include(&#8216;grid.css&#8217;);   
include(&#8216;print.css&#8217;);   
include(&#8216;handheld.css&#8217;);   
ob_end_flush();   
?>  

转载请注明:IT路人 » PHP:压缩CSS代码

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn