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

  下面用php实现了一个对css进行压缩和解压缩的小程序,暂不适用于js的操作,通过这个案例可以学习php字符替换和正则替换的技术.
 
  将css代码压缩能够减小文件的体积,从而减小了网络传输量和带宽占用,减小了服务器的处理的压力.
 
  代码如下:
 
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  
  <html xmlns="http://www.w3.org/1999/xhtml">
  
  <head>
  
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  
  </head>
  
  <?php  
  
  $string = trim(stripslashes($_POST['code'])); //stripslashes()函数删除转义字符(反斜杠)
  
  if(!emptyempty($string)){
  
  if($_POST['method'] == '压缩' ){
  
  $string = css_compress($string);
  
  }elseif($_POST['method'] == '解压缩' ){
  
  $string = css_decompress($string);
  
  }
  
  }else{
  
  $string = '';
  
  }
  
  function css_compress($string){
  
  //压缩
  
  $string = str_replace("/r/n","",$string); //首先去掉换行
  
  $string = preg_replace("/(/s*/{/s*)/","{",$string);
  
  $string = preg_replace("/(/s*/;/s*

dawei

【声明】:南充站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。