PHP implementation of the method of compressing multiple CSS and JS files, _php tutorial

Source: Internet
Author: User

PHP implementation of the compression of multiple CSS and JS file method,


This paper describes the PHP implementation of the compression of multiple CSS and JS file method. Share to everyone for your reference. The implementation method is as follows:

1. Compress CSS

Copy the Code code as follows: <?php
Header (' content-type:text/css ');
Ob_start ("compress");
function Compress ($buffer) {
/* Remove Comments */
$buffer = Preg_replace ('!/\*[^*]*\*+ ([^/][^*]*\*+) */! ', ' ', $buffer);
/* Remove tabs, spaces, newlines, etc. */
$buffer = Str_replace ("r \ n", "\ r", "\ n", "\ T", "', '", "),", $buffer);
return $buffer;
}

/* Your CSS files */
Include (' galleria.css ');
Include (' articles.css ');

Ob_end_flush ();
Here's how to use it:
Copy the Code code as follows: test

2. Compress JS, use Jsmin class:

This example originates from: http://code.google.com/p/minify/
Copy the Code code as follows: Header (' Content-type:text/javascript ');
Require ' jsmin.php ';
Echo jsmin::minify (file_get_contents (' Common.js '). file_get_contents (' common2.js '));

Where the jsmin.php file is as follows:

Copy CodeThe code 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 has the following license:
*
* --
* Copyright (c) 2002 Douglas Crockford (www.crockford.com)
*
* Permission is hereby granted, free of charge, to all 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 are furnished to do
* 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 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 a 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
* @copyright 2002 Douglas Crockford (JSMIN.C)
* @copyright Ryan Grove (PHP Port)
* @copyright Adam Goforth (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 __construct ()
* @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 $input Javascript to be 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'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 literal
*-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->inputindex, 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)) {
$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->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 used to see
* 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 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 {}
?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/909338.html www.bkjia.com true http://www.bkjia.com/PHPjc/909338.html techarticle PHP Implementation of the compression of multiple CSS and JS file method, this article describes the PHP implementation of the compression of multiple CSS and JS file method. Share to everyone for your reference. The specific implementation method is as follows: 1. Pressure ...

  • 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.