PHP implementation of compression Merge JS method

Source: Internet
Author: User
This article mainly introduces the PHP implementation of compression combined JS method, involving PHP compressed file class Jsmin related calls and use skills, and with the full demo source for the reader to download the reference, the need for friends can refer to the next

The test.php file is as follows:

Require_once (' jsmin.php '); $files = Glob ("Js/*.js"); $js = ""; foreach ($files as $file) {  $js. = Jsmin::minify (file_ Get_contents ($file));} File_put_contents ("Combined.js", $js); echo "Success";

The jsmin.php file is as follows:

<?php/** * jsmin.php-php Implementation of Douglas Crockford ' s jsmin. * * This was 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 have the Follo Wing License: * *--* Copyright (c) 2002 Douglas Crockford (www.crockford.com) * Permission is hereby granted, free of Charge, to 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, Distri Bute, sublicense, and/or sell copies * of the software, and to permit persons to whom the software are furnished to does * so , subject to the following Conditions: * * The above copyright notice and this permission notice shall is included in all * copies or substantial portions of the software. * * The Software shall is used for good, not Evil.  * * The software is provided ' as is ', without WARRANTY of any KIND, EXPRESS OR * implied, including and not LIMITED to the Warranties of merchantability, * FITNESS for A particular PURPOSE and noninfringement. In NO EVENT shall the * AUTHORS or COPYRIGHT holders is 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 Deali NGS in the * software. *--* * * @package jsmin * @author Ryan Grove <ryan@wonko.com> * @copyright 2002 Douglas Crockford &LT;DOUGLAS@CROCKF Ord.com> (JSMIN.C) * @copyright Ryan Grove <ryan@wonko.com> (PHP Port) * @copyright Adam Goforth <aa G@adamgoforth.com> (Updates) * @license http://opensource.org/licenses/mit-license.phP MIT License * @version 1.1.2 (2012-05-01) * @link https://github.com/rgrove/jsmin-php */class jsmin {const ORD_LF = 10; Const ORD_SPACE = 32; Const ACTION_KEEP_A = 1; Const ACTION_DELETE_A = 2; Const ACTION_DELETE_A_B = 3; protected $a = "; protected $b = "; protected $input = "; protected $inputIndex = 0; protected $inputLength = 0; protected $lookAhead = null; protected $output = "; --Public Static Methods--------------------------------------------------/** * minify Javascript * * @uses __cons Truct () * @uses min () * @param string $js Javascript to be minified * @return String */public static function Minify (  $JS) {$jsmin = new jsmin ($JS); return $jsmin->min (); }//--Public Instance Methods------------------------------------------------/** * Constructor * * @param string $i Nput Javascript to IS minified */Public function __construct ($input) {$this->input = str_replace ("\ r \ n", "\ nthe", $  Input); $this->inputlength = strlen ($this->input); }//--Protected Instance Methods---------------------------------------------/** * Action--do something!  What's determined by the $command argument. * * Action treats a string as a single character.  wow!  * Action recognizes a regular expression if it is preceded by (or, or =. * * @uses Next () * @uses get () * @throws jsminexception If parser Errors is found: *-unterminated string Litera  L *-unterminated regular expression set in regex literal *-unterminated regular expression literal * @param  int $command one of class constants: * Action_keep_a Output A. Copy b to A. Get the next B.  * Action_delete_a Copy B to A. Get the next B. (DELETE A). * Action_delete_a_b Get the next B. (DELETE b). */Protected function Action ($command) {switch ($command) {case self::action_keep_a: $this->output. = $this   A    Case SELF::ACTION_DELETE_A: $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 self::action_delete_a_b: $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->a = = = ' {' | | $this->a = = = '} ' | |      $this->a = = = '; ' | |     $this->a = = = "\ n")) {$this->output. = $this->a. $this->b; for (;;)      {$this->a = $this->get (); if ($this->a = = = ' [') {/*        Inside a regex [...] set, which may contain a '/' itself. Example:mootools Form.validator near line 460:return Form.Validator.getValidator (' IsEmpty '). Test (Element) | | (/^ (?:[a-z0-9!#$%& ' *+/=?^_ ' {|} ~-]\.?) {0,63} [a-z0-9!#$%& ' *+/=?^_ ' {|} ~-]@ (?:(?: [A-z0-9] (?: [A-z0-9-]{0,61}[a-z0-9])?.) *[a-z0-9] (?: [a-z0-9-]{0,61}[a-z0-9])? | \[(?:(? : 25[0-5]|2[0-4][0-9]| [01]? [0-9] [0-9]?) \.) {3} (?: 25[0-5]|2[0-4][0-9]| [01]? [0-9] [0-9]?)       \]) $/i). Test (Element.get (' value ')); */for (;;)        {$this->output. = $this->a;        $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 set in        Regex literal. ');      }}} elseif ($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 ();  }}}/** * Get next char. Convert Ctrl char to space.  * * @return String|null */protected function get () {$c = $this->lookahead;  $this->lookahead = null; if ($c = = = null) {if ($this->inputindex < $this->inputlength) {$c = substr ($this->input, $this->inpu    Tindex, 1);   $this->inputindex + = 1;   } else {$c = null;  }} if ($c = = = "\ r") {return "\ n");  if ($c = = = NULL | | $c = = = "\ n" | | ord ($c) >= self::ord_space) {return $c; } return ';  }/** * is $c a letter, digit, underscore, dollar sign, or non-ascii character. * * @return BOOL */protected function Isalphanum ($c) {return ord ($c) > 126 | | $c = = = ' \ \ ' | | preg_match('/^[\w\$]$/', $c) = = 1; }/** * Perform minification, return result * * @uses action () * @uses isalphanum () * @uses get () * @uses peek () *    @return String */protected function min () {if (0 = = strncmp ($this->peek (), "\xef", 1)) {$this->get ();    $this->get ();  $this->get ();  } $this->a = "\ n";  $this->action (Self::action_delete_a_b); while ($this->a!== null) {switch ($this->a) {case ': if ($this->isalphanum ($this->b)) {$th     Is->action (self::action_keep_a);     } else {$this->action (self::action_delete_a);    } break;  Case "\ n": Switch ($this->b) {case ' {': Case ' [': Case ' (': Case ' + ': Case '-': case       '! ': Case ' ~ ': $this->action (self::action_keep_a);      Break       Case ': $this->action (Self::action_delete_a_b);      Break     Default:if ($this->isalphanum ($this->b)) {$this->action (self::action_keep_a);  } else {$this->action (self::action_delete_a);    }} break; Default:switch ($this->b) {case ': if ($this->isalphanum ($this->a)) {$this->action (        SELF::ACTION_KEEP_A);       Break       } $this->action (Self::action_delete_a_b);      Break Case "\ n": Switch ($this->a) {case '} ': Case '] ': Case ') ': Case ' + ': Case '-         ': Case ', ': Case ' ': $this->action (self::action_keep_a);        Break         Default:if ($this->isalphanum ($this->a)) {$this->action (self::action_keep_a);         } else {$this->action (self::action_delete_a_b);      }} break;       Default: $this->action (self::action_keep_a);     Break }}} return $this->output; }/** * Get the next character, skipping over comments.  Peek () is the used to see * if a '/' was followed by a '/' or ' * '.* * @uses get () * @uses peek () * @throws jsminexception on unterminated comment.  * @return String */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; }/** * Get next char.  If is the CTRL character, translate to a space or newline.  * * @uses get () * @return String|null */protected function peek () {$this->lookahead = $this->get (); return $this->lookahead; }}//--Exceptions---------------------------------------------------------------class Jsminexception extends ExCeption {}?> 

Summary: The above is the entire content of this article, I hope to be able to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.