一聚教程网:一个值得你收藏的教程网站

热门教程

php实现对css文件进行压缩和解压缩

时间:2022-06-24 17:32:28 编辑:袖梨 来源:一聚教程网

css代码压缩能够减小文件的体积,从而减小了网络传输量和带宽占用,减小了服务器的处理的压力


源码:

 代码如下 复制代码


$string = trim(stripslashes($_POST['code'])); //stripslashes()函数删除转义字符(反斜杠)

if(!empty($string)){

if($_POST['method'] == '压缩' ){

$string = css_compress($string);

}elseif($_POST['method'] == '解压缩' ){

$string = css_decompress($string);

}

}else{

$string = '';

}

function css_compress($string){

//压缩

$string = str_replace("rn","",$string); //首先去掉换行

$string = preg_replace("/(s*{s*)/","{",$string);

$string = preg_replace("/(s*;s*}s*)/","}",$string); //去掉反括号首位的空格和换行,和最后一个;

$string = preg_replace("/(s*;s*)/",";",$string);

return $string;

}

function css_decompress($string){

//解压

$string = css_compress($string); //为了效果更好,解压前,先压缩至最简状态

$string = str_replace("{","rn{rnt",$string);

$string = str_replace("}","rn}rnrn",$string); 

$string = str_replace(";",";rnt",$string);

$string = str_replace("*/","*/rn",$string);

return $string;

}

?>

 

 

请将css代码粘贴到下面框中,然后选择压缩/解压缩

 

    <textarea style="width:90%;height:460px;padding:5px;" name="code">

   

   

   

 

 

热门栏目