This article describes the implementation of PHP compression merge JS method. Share to everyone for your reference, specific as follows:
test.php files are 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";
jsmin.php files are as follows:
<?php/** * jsmin.php-php implementation of Douglas Crockford ' s jsmin. * * This are pretty much a direct port of JSMIN.C to PHP with just a few * php-specific performance. Also, whereas jsmin.c reads from STDIN and * outputs to stdout, this library accepts a string as input and returns anothe
R * 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 fol lowing License: * Copyright (c) 2002 Douglas Crockford (www.crockford.com) * * Permission is hereby granted, F Ree 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, Publi SH, distribute, sublicense, and/or sell copies * of the Software, and to permit persons to whom the Software is furnished To did * so, subject To the following conditions: * * The above of copyright notice and this permission notice the shall is included in all * Copi
Es 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 BUT no LIMITED to T He warranties of the merchantability, * FITNESS for A particular purpose and noninfringement. In NO EVENT SHALL the * AUTHORS or COPYRIGHT holders is liable for all CLAIM, damages or other * liability, WHETHER in A N ACTION of CONTRACT, TORT or otherwise, arising from, * out of OR in CONNECTION with the SOFTWARE or the "or" other DE
Alings in the * SOFTWARE. *-* @package jsmin * @author Ryan Grove <ryan@wonko.com> * @copyright 2002 Douglas Crockford <DOUGLAS@CR Ockford.com> (JSMIN.C) * @copyright 2008 Ryan Grove <ryan@wonko.com> (PHP Port) * @copyright Adam Goforth <aag@adamgoforth.com> (Updates) * @license http://opensource.org/licenses/mit-license.php mit License * @version 1.1.2 (2012-05-01) * @link Https://github.com/rgro
ve/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 __ Construct () * @uses min () * @param string $js Javascript to is minified * @return String */public static Functio
n Minify ($js) {$jsmin = new jsmin ($JS);
return $jsmin->min (); }//--Public Instance Methods------------------------------------------------/** * constructor * * @param stri Ng $input Javascript to is minified/Public function __construct ($input) {
$this->input = Str_replace ("\ r \ n", "\ n", $input);
$this->inputlength = strlen ($this->input); }//--Protected Instance Methods---------------------------------------------/** * Action-do something!
What to what 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 are found: *-Unterminated string li Teral *-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 to expression.
');
$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->in
Putindex, 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 pee K () * @return string/protected function min () {if (0 = strncmp ($this->peek (), "\xef", 1)) {$this->g
ET ();
$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)) {
$this->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->act
Ion (SELF::ACTION_KEEP_A);
Break
} $this->action (Self::action_delete_a_b);
Break Case "\ n": Switch ($this->a) {case '} ': Case '] ': Case ') ': Case ' + ': C
Ase '-': 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 and skipping over comments.
Peek () is used to = if a '/' is 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 CTRL character, translate to a or newline.
* @uses get () * @return string|null * * protected function Peek () {$this->lookahead = $this->get ();
return $this->lookahead; }//--Exceptions---------------------------------------------------------------class Jsminexception extends Exception {}?>
Full instance code click here to download the site .
More about PHP Interested readers can view the site topics: "PHP file Operation Summary", "PHP Directory Operation skills Summary", "PHP Basic Grammar Primer", "PHP operation and operator Usage Summary", "PHP object-oriented Programming Program", " PHP Network Programming skills Summary, "PHP string (String) Usage summary", "PHP Array" Operation Tips Encyclopedia, "php+mysql database Operation Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design.