当前位置:380元网站建设 虎网站 > seo 优化 > 正文

php压缩css和js

发布日期:2014-06-23

首先你要知道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基础的教程

韩文博的新浪博客:http://blog.sina.com.cn/u/1783136603

更多网站建设方面的教程可以看经常来看我们的官网,频繁更新中,天津网站建设 www.huwz.com

下面是JSMin类

<?php

/**
 * jsmin.php - PHP implementation of Douglas Crockford's JSMin.
 *
 * This is pretty much a direct port of jsmin.c to PHP with just a few
 * PHP-specific performance tweaks. Also, whereas jsmin.c reads from stdin and
 * outputs to stdout, this library accepts a string as input and returns another
 * string as output.
 *
 * PHP 5 or higher is required.
 *
 * Permission is hereby granted to use this version of the library under the
 * same terms as jsmin.c, which has the following license:
 *
 * --
 * Copyright (c) 2002 Douglas Crockford  (www.crockford.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * The Software shall be used for Good, not Evil.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * --
 *
 * @package JSMin
 * @author Ryan Grove <ryan@wonko.com>
 * @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
 * @copyright 2008 Ryan Grove <ryan@wonko.com> (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('Unterminated string literal.');
            }

            if ($this->a === '\\') {
              $this->output .= $this->a;
              $this->a       = $this->get();
            }
          }
        }

      case 3:
        $this->b = $this->next();

        if ($this->b === '/' && (
            $this->a === '(' || $this->a === ',' || $this->a === '=' ||
            $this->a === ':' || $this->a === '[' || $this->a === '!' ||
            $this->a === '&' || $this->a === '|' || $this->a === '?')) {

          $this->output .= $this->a . $this->b;

          for (;;) {
            $this->a = $this->get();

            if ($this->a === '/') {
              break;
            } elseif ($this->a === '\\') {
              $this->output .= $this->a;
              $this->a       = $this->get();
            } elseif (ord($this->a) <= self::ORD_LF) {
              throw new JSMinException('Unterminated regular expression '.
                  'literal.');
            }

            $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 ' ';
  }

  protected function isAlphaNum($c) {
    return ord($c) > 126 || $c === '\\' || preg_match('/^[\w\$]$/', $c) === 1;
  }

  protected function min() {
    $this->a = "\n";
    $this->action(3);

    while ($this->a !== null) {
      switch ($this->a) {
        case ' ':
          if ($this->isAlphaNum($this->b)) {
            $this->action(1);
          } else {
            $this->action(2);
          }
          break;

        case "\n":
          switch ($this->b) {
            case '{':
            case '[':
            case '(':
            case '+':
            case '-':
              $this->action(1);
              break;

            case ' ':
              $this->action(3);
              break;

            default:
              if ($this->isAlphaNum($this->b)) {
                $this->action(1);
              }
              else {
                $this->action(2);
              }
          }
          break;

        default:
          switch ($this->b) {
            case ' ':
              if ($this->isAlphaNum($this->a)) {
                $this->action(1);
                break;
              }

              $this->action(3);
              break;

            case "\n":
              switch ($this->a) {
                case '}':
                case ']':
                case ')':
                case '+':
                case '-':
                case '"':
                case "'":
                  $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 === '/') {
      switch($this->peek()) {
        case '/':
          for (;;) {
            $c = $this->get();

            if (ord($c) <= self::ORD_LF) {
              return $c;
            }
          }

        case '*':
          $this->get();

          for (;;) {
            switch($this->get()) {
              case '*':
                if ($this->peek() === '/') {
                  $this->get();
                  return ' ';
                }
                break;

              case null:
                throw new JSMinException('Unterminated comment.');
            }
          }

        default:
          return $c;
      }
    }

    return $c;
  }

  protected function peek() {
    $this->lookAhead = $this->get();
    return $this->lookAhead;
  }
}

// -- Exceptions ---------------------------------------------------------------
class JSMinException {}
//class JSMinException extends Exception {}
?>

迪拜旅游费用一般是多少,去迪拜要多少钱啊?

    去迪拜旅游大概花多少钱? 去迪拜旅游,花多少钱,不能简单一个数字来说。 多有多的玩法,少有少的玩法。 如今大部分人还是以跟团游为主,目前市场上跟团游基本以购物旅游为主,就是跟国内香港的操作方式差不多。就是一购物为重点,旅行社基本从购物返点来盈利,因为团价看起来非常的低。只是旅游的体验感可能就没那么理想,如果还没出过国或者很少出国的朋友可以通过这种方式,目前这种市场上价格在4500-6000之前,...

       企业网站设计网页设计动图绘制系统 互联网改变传统生活方式, 在获取信息及人际交往过程中, 已经产生质变。这种改变成为社会发展的动力, 各种思路与技术的融合, 使得图形设计自静态转变为动态, 催生新型设计科目, 动态图形设计。现阶段网络技术已经覆盖人们生活的各个层面, 人们活动已经无法脱离网络。Web网页是人们意识中的虚拟概念, 用户不需要协议便能够了解信息。直观交互方式形成独特的风格, 网站页面设计是其中重点, 成功商业网站, 需...

       美容养生网站建设一切功能说明 美容养生网站网站访问人群浏览网站目的可归纳为以下几点:   (1).查看企业背景、企业动态等信息,了解企业近期优惠活动,了解企业最新的动态。   (2).搜索相关产品、服务信息,获取企业产品详细介绍,加深对企业产品的了解。   (3).通过网站在线购物系统,在线定购化妆品、护理服务。在线购买会员卡、在线为会员卡续费。   (4).获取其他相关信息,如美容知识、加盟信息、服务信息、联系信息等。   ...

       网站设计哪些要素可以加深客户印象 企业要怎样才能让客户加深对网站的印象呢?网站印象表现出来的也是企业形象,网站风格设计影响着企业的形象,虽然对网站风格没有固定的定位,但可以参考同行网站并加入自己的独特元素使网站表现更具特色。同时在网站里添加实用性高的内容,再结合有特色的网站设计能够与客户产生粘性,从而加深客户印象。接下来针对这几个要素进行分析。 网站设计哪些要素可以加深客户印象   要素一:定位风格   在设计之前就要对网站定位,...

       怎样让网站实现盈利 第一,我们要确立好网站的发展方向和网站所要面对的主要人群。我们要做网站就必须要明确自己的网站所要确立的发展方向主要内容,以及我们网站所要面对的是什么样的人群。这要我们接下来就要分析围绕这个网站的主要核心,如何去构建这个网站,以及去确立网站的关键字。分析网站所要面临的主要是那些人群客户,接着我们去站在这些人群客户的角度上去分析和了解,到底这些人需要什么,什么才是他们最关心的,以及我们的网站怎么才能为...

       网页制作的小技巧,快速提高转化率! 据 天津网站建设 所知,很多的企业网站建设成功以后,就开始不停的发外链,发内链,做友链、写文章,一门心思的想着把网站关键词优化到首页,但当网站真的有了排名、有了流量却发现并非产生订单,这可能是当前很多的企业的通病。其主要原因在于,站长过度的在于网站的流量和排名,却忽视了对网站最基本东西的优化,而用户所需要的正式这基本的东西,在信息爆炸的时代,用户每一个点击都很珍贵,若我们好不容易引来的流量未得到有...

最新模板
少儿英语培训机构网站建设 培训公司网站建设
成人自考网站建设 成人培训网站制作设计版面
智能家居摆设设计网站建设 智能家居装修企业网站建设
铜螺母,铜螺丝,螺栓,螺钉工厂网站建设
废气处理设备,如活性炭吸附箱、RTO蓄热式氧化处理设备、光氧等离子复合设备 低温等离子设备 光氧催化设备等网企业网站建设
图文阅读