热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

php压缩css和js类-PHP源码

php压缩css和js
首先你要知道php 《JSMin》 这个类,压缩js很方便
css直接去回车就行了
file_put_contents(存放路径,str_replace(array("\r\n", "\r", "\n"), "", file_get_contents(要压缩的css文件路径)));
$Jsmin = new \Common\Extend\Jsmin();
file_put_contents(存放路径, $Jsmin->minify(file_get_contents(要压缩的js路径)) );
如有不明白的可以看我的博客,里面有一些php、js、css基础的教程

1.代码

下面是JSMin类

 * @copyright 2002 Douglas Crockford  (jsmin.c)
 * @copyright 2008 Ryan Grove  (PHP port)
 * @license http://opensource.org/licenses/mit-license.php MIT License
 * @version 1.1.1 (2008-03-02)
 * @link http://code.google.com/p/jsmin-php/
 */
namespace Common\Extend;
class JSMin {
  const ORD_LF    = 10;
  const ORD_SPACE = 32;
 
  protected $a           = '';
  protected $b           = '';
  protected $input       = '';
  protected $inputIndex  = 0;
  protected $inputLength = 0;
  protected $lookAhead   = null;
  protected $output      = '';
 
  // -- Public Static Methods --------------------------------------------------
 
  public static function minify($js) {
    $jsmin = new JSMin($js);
    return $jsmin->min();
  }
 
  // -- Public Instance Methods ------------------------------------------------
 
  public function __construct($input) {
    $this->input       = str_replace("\r\n", "\n", $input);
    $this->inputLength = strlen($this->input);
  }
 
  // -- Protected Instance Methods ---------------------------------------------
 
  protected function action($d) {
    switch($d) {
      case 1:
        $this->output .= $this->a;
 
      case 2:
        $this->a = $this->b;
 
        if ($this->a === "'" || $this->a === '"') {
          for (;;) {
            $this->output .= $this->a;
            $this->a       = $this->get();
 
            if ($this->a === $this->b) {
              break;
            }
 
            if (ord($this->a) <= self::ORD_LF) {
              throw new JSMinException(&#39;Unterminated string literal.&#39;);
            }
 
            if ($this->a === &#39;\\&#39;) {
              $this->output .= $this->a;
              $this->a       = $this->get();
            }
          }
        }
 
      case 3:
        $this->b = $this->next();
 
        if ($this->b === &#39;/&#39; && (
            $this->a === &#39;(&#39; || $this->a === &#39;,&#39; || $this->a === &#39;=&#39; ||
            $this->a === &#39;:&#39; || $this->a === &#39;[&#39; || $this->a === &#39;!&#39; ||
            $this->a === &#39;&&#39; || $this->a === &#39;|&#39; || $this->a === &#39;?&#39;)) {
 
          $this->output .= $this->a . $this->b;
 
          for (;;) {
            $this->a = $this->get();
 
            if ($this->a === &#39;/&#39;) {
              break;
            } elseif ($this->a === &#39;\\&#39;) {
              $this->output .= $this->a;
              $this->a       = $this->get();
            } elseif (ord($this->a) <= self::ORD_LF) {
              throw new JSMinException(&#39;Unterminated regular expression &#39;.
                  &#39;literal.&#39;);
            }
 
            $this->output .= $this->a;
          }
 
          $this->b = $this->next();
        }
    }
  }
 
  protected function get() {
    $c = $this->lookAhead;
    $this->lookAhead = null;
 
    if ($c === null) {
      if ($this->inputIndex <$this->inputLength) {
        $c = $this->input[$this->inputIndex];
        $this->inputIndex += 1;
      } else {
        $c = null;
      }
    }
 
    if ($c === "\r") {
      return "\n";
    }
 
    if ($c === null || $c === "\n" || ord($c) >= self::ORD_SPACE) {
      return $c;
    }
 
    return &#39; &#39;;
  }
 
  protected function isAlphaNum($c) {
    return ord($c) > 126 || $c === &#39;\\&#39; || preg_match(&#39;/^[\w\$]$/&#39;, $c) === 1;
  }
 
  protected function min() {
    $this->a = "\n";
    $this->action(3);
 
    while ($this->a !== null) {
      switch ($this->a) {
        case &#39; &#39;:
          if ($this->isAlphaNum($this->b)) {
            $this->action(1);
          } else {
            $this->action(2);
          }
          break;
 
        case "\n":
          switch ($this->b) {
            case &#39;{&#39;:
            case &#39;[&#39;:
            case &#39;(&#39;:
            case &#39;+&#39;:
            case &#39;-&#39;:
              $this->action(1);
              break;
 
            case &#39; &#39;:
              $this->action(3);
              break;
 
            default:
              if ($this->isAlphaNum($this->b)) {
                $this->action(1);
              }
              else {
                $this->action(2);
              }
          }
          break;
 
        default:
          switch ($this->b) {
            case &#39; &#39;:
              if ($this->isAlphaNum($this->a)) {
                $this->action(1);
                break;
              }
 
              $this->action(3);
              break;
 
            case "\n":
              switch ($this->a) {
                case &#39;}&#39;:
                case &#39;]&#39;:
                case &#39;)&#39;:
                case &#39;+&#39;:
                case &#39;-&#39;:
                case &#39;"&#39;:
                case "&#39;":
                  $this->action(1);
                  break;
 
                default:
                  if ($this->isAlphaNum($this->a)) {
                    $this->action(1);
                  }
                  else {
                    $this->action(3);
                  }
              }
              break;
 
            default:
              $this->action(1);
              break;
          }
      }
    }
 
    return $this->output;
  }
 
  protected function next() {
    $c = $this->get();
 
    if ($c === &#39;/&#39;) {
      switch($this->peek()) {
        case &#39;/&#39;:
          for (;;) {
            $c = $this->get();
 
            if (ord($c) <= self::ORD_LF) {
              return $c;
            }
          }
 
        case &#39;*&#39;:
          $this->get();
 
          for (;;) {
            switch($this->get()) {
              case &#39;*&#39;:
                if ($this->peek() === &#39;/&#39;) {
                  $this->get();
                  return &#39; &#39;;
                }
                break;
 
              case null:
                throw new JSMinException(&#39;Unterminated comment.&#39;);
            }
          }
 
        default:
          return $c;
      }
    }
 
    return $c;
  }
 
  protected function peek() {
    $this->lookAhead = $this->get();
    return $this->lookAhead;
  }
}
 
// -- Exceptions ---------------------------------------------------------------
class JSMinException {}
//class JSMinException extends Exception {}
?>

2.php代码


 * @copyright 2002 Douglas Crockford  (jsmin.c)
 * @copyright 2008 Ryan Grove  (PHP port)
 * @license http://opensource.org/licenses/mit-license.php MIT License
 * @version 1.1.1 (2008-03-02)
 * @link http://code.google.com/p/jsmin-php/
 */
namespace Common\Extend;
class JSMin {
  const ORD_LF    = 10;
  const ORD_SPACE = 32;
 
  protected $a           = &#39;&#39;;
  protected $b           = &#39;&#39;;
  protected $input       = &#39;&#39;;
  protected $inputIndex  = 0;
  protected $inputLength = 0;
  protected $lookAhead   = null;
  protected $output      = &#39;&#39;;
 
  // -- Public Static Methods --------------------------------------------------
 
  public static function minify($js) {
    $jsmin = new JSMin($js);
    return $jsmin->min();
  }
 
  // -- Public Instance Methods ------------------------------------------------
 
  public function __construct($input) {
    $this->input       = str_replace("\r\n", "\n", $input);
    $this->inputLength = strlen($this->input);
  }
 
  // -- Protected Instance Methods ---------------------------------------------
 
  protected function action($d) {
    switch($d) {
      case 1:
        $this->output .= $this->a;
 
      case 2:
        $this->a = $this->b;
 
        if ($this->a === "&#39;" || $this->a === &#39;"&#39;) {
          for (;;) {
            $this->output .= $this->a;
            $this->a       = $this->get();
 
            if ($this->a === $this->b) {
              break;
            }
 
            if (ord($this->a) <= self::ORD_LF) {
              throw new JSMinException(&#39;Unterminated string literal.&#39;);
            }
 
            if ($this->a === &#39;\\&#39;) {
              $this->output .= $this->a;
              $this->a       = $this->get();
            }
          }
        }
 
      case 3:
        $this->b = $this->next();
 
        if ($this->b === &#39;/&#39; && (
            $this->a === &#39;(&#39; || $this->a === &#39;,&#39; || $this->a === &#39;=&#39; ||
            $this->a === &#39;:&#39; || $this->a === &#39;[&#39; || $this->a === &#39;!&#39; ||
            $this->a === &#39;&&#39; || $this->a === &#39;|&#39; || $this->a === &#39;?&#39;)) {
 
          $this->output .= $this->a . $this->b;
 
          for (;;) {
            $this->a = $this->get();
 
            if ($this->a === &#39;/&#39;) {
              break;
            } elseif ($this->a === &#39;\\&#39;) {
              $this->output .= $this->a;
              $this->a       = $this->get();
            } elseif (ord($this->a) <= self::ORD_LF) {
              throw new JSMinException(&#39;Unterminated regular expression &#39;.
                  &#39;literal.&#39;);
            }
 
            $this->output .= $this->a;
          }
 
          $this->b = $this->next();
        }
    }
  }
 
  protected function get() {
    $c = $this->lookAhead;
    $this->lookAhead = null;
 
    if ($c === null) {
      if ($this->inputIndex <$this->inputLength) {
        $c = $this->input[$this->inputIndex];
        $this->inputIndex += 1;
      } else {
        $c = null;
      }
    }
 
    if ($c === "\r") {
      return "\n";
    }
 
    if ($c === null || $c === "\n" || ord($c) >= self::ORD_SPACE) {
      return $c;
    }
 
    return &#39; &#39;;
  }
 
  protected function isAlphaNum($c) {
    return ord($c) > 126 || $c === &#39;\\&#39; || preg_match(&#39;/^[\w\$]$/&#39;, $c) === 1;
  }
 
  protected function min() {
    $this->a = "\n";
    $this->action(3);
 
    while ($this->a !== null) {
      switch ($this->a) {
        case &#39; &#39;:
          if ($this->isAlphaNum($this->b)) {
            $this->action(1);
          } else {
            $this->action(2);
          }
          break;
 
        case "\n":
          switch ($this->b) {
            case &#39;{&#39;:
            case &#39;[&#39;:
            case &#39;(&#39;:
            case &#39;+&#39;:
            case &#39;-&#39;:
              $this->action(1);
              break;
 
            case &#39; &#39;:
              $this->action(3);
              break;
 
            default:
              if ($this->isAlphaNum($this->b)) {
                $this->action(1);
              }
              else {
                $this->action(2);
              }
          }
          break;
 
        default:
          switch ($this->b) {
            case &#39; &#39;:
              if ($this->isAlphaNum($this->a)) {
                $this->action(1);
                break;
              }
 
              $this->action(3);
              break;
 
            case "\n":
              switch ($this->a) {
                case &#39;}&#39;:
                case &#39;]&#39;:
                case &#39;)&#39;:
                case &#39;+&#39;:
                case &#39;-&#39;:
                case &#39;"&#39;:
                case "&#39;":
                  $this->action(1);
                  break;
 
                default:
                  if ($this->isAlphaNum($this->a)) {
                    $this->action(1);
                  }
                  else {
                    $this->action(3);
                  }
              }
              break;
 
            default:
              $this->action(1);
              break;
          }
      }
    }
 
    return $this->output;
  }
 
  protected function next() {
    $c = $this->get();
 
    if ($c === &#39;/&#39;) {
      switch($this->peek()) {
        case &#39;/&#39;:
          for (;;) {
            $c = $this->get();
 
            if (ord($c) <= self::ORD_LF) {
              return $c;
            }
          }
 
        case &#39;*&#39;:
          $this->get();
 
          for (;;) {
            switch($this->get()) {
              case &#39;*&#39;:
                if ($this->peek() === &#39;/&#39;) {
                  $this->get();
                  return &#39; &#39;;
                }
                break;
 
              case null:
                throw new JSMinException(&#39;Unterminated comment.&#39;);
            }
          }
 
        default:
          return $c;
      }
    }
 
    return $c;
  }
 
  protected function peek() {
    $this->lookAhead = $this->get();
    return $this->lookAhead;
  }
}
 
// -- Exceptions ---------------------------------------------------------------
class JSMinException {}
//class JSMinException extends Exception {}
?>

 * @copyright 2002 Douglas Crockford  (jsmin.c)
 * @copyright 2008 Ryan Grove  (PHP port)
 * @license http://opensource.org/licenses/mit-license.php MIT License
 * @version 1.1.1 (2008-03-02)
 * @link http://code.google.com/p/jsmin-php/
 */
namespace Common\Extend;
class JSMin {
  const ORD_LF    = 10;
  const ORD_SPACE = 32;
 
  protected $a           = &#39;&#39;;
  protected $b           = &#39;&#39;;
  protected $input       = &#39;&#39;;
  protected $inputIndex  = 0;
  protected $inputLength = 0;
  protected $lookAhead   = null;
  protected $output      = &#39;&#39;;
 
  // -- Public Static Methods --------------------------------------------------
 
  public static function minify($js) {
    $jsmin = new JSMin($js);
    return $jsmin->min();
  }
 
  // -- Public Instance Methods ------------------------------------------------
 
  public function __construct($input) {
    $this->input       = str_replace("\r\n", "\n", $input);
    $this->inputLength = strlen($this->input);
  }
 
  // -- Protected Instance Methods ---------------------------------------------
 
  protected function action($d) {
    switch($d) {
      case 1:
        $this->output .= $this->a;
 
      case 2:
        $this->a = $this->b;
 
        if ($this->a === "&#39;" || $this->a === &#39;"&#39;) {
          for (;;) {
            $this->output .= $this->a;
            $this->a       = $this->get();
 
            if ($this->a === $this->b) {
              break;
            }
 
            if (ord($this->a) <= self::ORD_LF) {
              throw new JSMinException(&#39;Unterminated string literal.&#39;);
            }
 
            if ($this->a === &#39;\\&#39;) {
              $this->output .= $this->a;
              $this->a       = $this->get();
            }
          }
        }
 
      case 3:
        $this->b = $this->next();
 
        if ($this->b === &#39;/&#39; && (
            $this->a === &#39;(&#39; || $this->a === &#39;,&#39; || $this->a === &#39;=&#39; ||
            $this->a === &#39;:&#39; || $this->a === &#39;[&#39; || $this->a === &#39;!&#39; ||
            $this->a === &#39;&&#39; || $this->a === &#39;|&#39; || $this->a === &#39;?&#39;)) {
 
          $this->output .= $this->a . $this->b;
 
          for (;;) {
            $this->a = $this->get();
 
            if ($this->a === &#39;/&#39;) {
              break;
            } elseif ($this->a === &#39;\\&#39;) {
              $this->output .= $this->a;
              $this->a       = $this->get();
            } elseif (ord($this->a) <= self::ORD_LF) {
              throw new JSMinException(&#39;Unterminated regular expression &#39;.
                  &#39;literal.&#39;);
            }
 
            $this->output .= $this->a;
          }
 
          $this->b = $this->next();
        }
    }
  }
 
  protected function get() {
    $c = $this->lookAhead;
    $this->lookAhead = null;
 
    if ($c === null) {
      if ($this->inputIndex <$this->inputLength) {
        $c = $this->input[$this->inputIndex];
        $this->inputIndex += 1;
      } else {
        $c = null;
      }
    }
 
    if ($c === "\r") {
      return "\n";
    }
 
    if ($c === null || $c === "\n" || ord($c) >= self::ORD_SPACE) {
      return $c;
    }
 
    return &#39; &#39;;
  }
 
  protected function isAlphaNum($c) {
    return ord($c) > 126 || $c === &#39;\\&#39; || preg_match(&#39;/^[\w\$]$/&#39;, $c) === 1;
  }
 
  protected function min() {
    $this->a = "\n";
    $this->action(3);
 
    while ($this->a !== null) {
      switch ($this->a) {
        case &#39; &#39;:
          if ($this->isAlphaNum($this->b)) {
            $this->action(1);
          } else {
            $this->action(2);
          }
          break;
 
        case "\n":
          switch ($this->b) {
            case &#39;{&#39;:
            case &#39;[&#39;:
            case &#39;(&#39;:
            case &#39;+&#39;:
            case &#39;-&#39;:
              $this->action(1);
              break;
 
            case &#39; &#39;:
              $this->action(3);
              break;
 
            default:
              if ($this->isAlphaNum($this->b)) {
                $this->action(1);
              }
              else {
                $this->action(2);
              }
          }
          break;
 
        default:
          switch ($this->b) {
            case &#39; &#39;:
              if ($this->isAlphaNum($this->a)) {
                $this->action(1);
                break;
              }
 
              $this->action(3);
              break;
 
            case "\n":
              switch ($this->a) {
                case &#39;}&#39;:
                case &#39;]&#39;:
                case &#39;)&#39;:
                case &#39;+&#39;:
                case &#39;-&#39;:
                case &#39;"&#39;:
                case "&#39;":
                  $this->action(1);
                  break;
 
                default:
                  if ($this->isAlphaNum($this->a)) {
                    $this->action(1);
                  }
                  else {
                    $this->action(3);
                  }
              }
              break;
 
            default:
              $this->action(1);
              break;
          }
      }
    }
 
    return $this->output;
  }
 
  protected function next() {
    $c = $this->get();
 
    if ($c === &#39;/&#39;) {
      switch($this->peek()) {
        case &#39;/&#39;:
          for (;;) {
            $c = $this->get();
 
            if (ord($c) <= self::ORD_LF) {
              return $c;
            }
          }
 
        case &#39;*&#39;:
          $this->get();
 
          for (;;) {
            switch($this->get()) {
              case &#39;*&#39;:
                if ($this->peek() === &#39;/&#39;) {
                  $this->get();
                  return &#39; &#39;;
                }
                break;
 
              case null:
                throw new JSMinException(&#39;Unterminated comment.&#39;);
            }
          }
 
        default:
          return $c;
      }
    }
 
    return $c;
  }
 
  protected function peek() {
    $this->lookAhead = $this->get();
    return $this->lookAhead;
  }
}
 
// -- Exceptions ---------------------------------------------------------------
class JSMinException {}
//class JSMinException extends Exception {}
?>

推荐阅读
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文内容为asp.net微信公众平台开发的目录汇总,包括数据库设计、多层架构框架搭建和入口实现、微信消息封装及反射赋值、关注事件、用户记录、回复文本消息、图文消息、服务搭建(接入)、自定义菜单等。同时提供了示例代码和相关的后台管理功能。内容涵盖了多个方面,适合综合运用。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文讲述了作者通过点火测试男友的性格和承受能力,以考验婚姻问题。作者故意不安慰男友并再次点火,观察他的反应。这个行为是善意的玩人,旨在了解男友的性格和避免婚姻问题。 ... [详细]
  • MACElasticsearch安装步骤及验证方法
    本文介绍了MACElasticsearch的安装步骤,包括下载ZIP文件、解压到安装目录、启动服务,并提供了验证启动是否成功的方法。同时,还介绍了安装elasticsearch-head插件的方法,以便于进行查询操作。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
author-avatar
爱上我承认了
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有