- 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 ();
- ?>
Copy CodeInstantiation: test.php
-
- Test
Copy Code2. Compressing JS using Jsmin class Source: http://code.google.com/p/minify/compress.php
- Header (' Content-type:text/javascript ');
- Require ' jsmin.php ';
- Echo jsmin::minify (file_get_contents (' Common.js '). file_get_contents (' common2.js '));
- ?>
Copy CodeCommon.jsalert (' first JS '); Common.jsalert (' second JS '); jsmin.php
- /**
- * jsmin.php-extended PHP implementation of Douglas Crockford ' s jsmin.
- *
- *
* $minifiedJs = JSMin::minify($js);
*
- *
- * This was a direct port of JSMIN.C to PHP with a few PHP performance tweaks and
- * Modifications to preserve some comments (see below). Also, rather than using
- * Stdin/stdout, Jsmin::minify () accepts a string as input and returns another
- * String as output.
- *
- * Comments containing IE conditional compilation is preserved, as is multi-line
- * Comments that begin with "/*!" (For documentation purposes). In the latter case
- * Newlines is inserted around the comment to enhance readability.
- *
- * 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 (PHP port)
- * @author Steve Clay (modifications + cleanup)
- * @author Andrea Giammarchi (spacebeforeregexp)
- * @copyright 2002 Douglas Crockford (JSMIN.C)
- * @copyright Ryan Grove (PHP port)
- * @license http://opensource.org/licenses/mit-license.php mit License
- * @link http://code.google.com/p/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 = "\ n";
- protected $b = ";
- protected $input = ";
- protected $inputIndex = 0;
- protected $inputLength = 0;
- protected $lookAhead = null;
- protected $output = ";
- /**
- * Minify Javascript
- *
- * @param string $js Javascript to be minified
- * @return String
- */
- public static function Minify ($JS)
- {
- Look out for syntax like "+ + +" and "-+ +"
- $p = ' \\+ ';
- $m = ' \\-';
- if (Preg_match ("/([$p $m]) (?: \ \1 [$p $m]| (?: $p $p| $m $m)) /", $js)) {
- Likely pre-minified and would be broken by jsmin
- return $JS;
- }
- $jsmin = new Jsmin ($JS);
- return $jsmin->min ();
- }
- /*
- * Don ' t create a jsmin instance, instead use the static function Minify,
- * Which checks for mb_string function overloading and avoids errors
- * Trying to re-minify the output of Closure Compiler
- *
- * @private
- */
- Public function __construct ($input)
- {
- $this->input = $input;
- }
- /**
- * Perform minification, return result
- */
- Public Function min ()
- {
- if ($this->output!== ") {//min already run
- return $this->output;
- }
- $MBINTENC = null;
- if (function_exists (' Mb_strlen ') && ((int) ini_get (' Mbstring.func_overload ') & 2)) {
- $MBINTENC = Mb_internal_encoding ();
- Mb_internal_encoding (' 8bit ');
- }
- $this->input = Str_replace ("\ r \ n", "\ n", $this->input);
- $this->inputlength = strlen ($this->input);
- $this->action (Self::action_delete_a_b);
- while ($this->a!== null) {
- Determine next command
- $command = self::action_keep_a; Default
- if ($this->a = = = ") {
- if (! $this->isalphanum ($this->b)) {
- $command = self::action_delete_a;
- }
- } elseif ($this->a = = = = "\ n") {
- if ($this->b = = = ") {
- $command = Self::action_delete_a_b;
- In case of Mbstring.func_overload & 2, must check for null B,
- Otherwise mb_strpos'll give WARNING
- } elseif ($this->b = = = NULL
- || (false = = = Strpos (' {[+ ', $this->b)
- &&! $this->isalphanum ($this->b))) {
- $command = self::action_delete_a;
- }
- } elseif (! $this->isalphanum ($this->a)) {
- if ($this->b = = = "
- || ($this->b = = = "\ n"
- && (false = = = Strpos ('}]) +-"\", $this->a))) {
- $command = Self::action_delete_a_b;
- }
- }
- $this->action ($command);
- }
- $this->output = Trim ($this->output);
- if ($MBINTENC!== null) {
- Mb_internal_encoding ($MBINTENC);
- }
- return $this->output;
- }
- /**
- * action_keep_a = Output A. Copy b to A. Get the next B.
- * Action_delete_a = Copy b to A. Get the next B.
- * Action_delete_a_b = Get the next B.
- */
- protected function Action ($command)
- {
- Switch ($command) {
- Case SELF::ACTION_KEEP_A:
- $this->output. = $this->a;
- Fallthrough
- Case SELF::ACTION_DELETE_A:
- $this->a = $this->b;
- if ($this->a = = = "'" | | $this->a = = = ' "') {//string literal
- $str = $this->a; In case needed for exception
- while (true) {
- $this->output. = $this->a;
- $this->a = $this->get ();
- if ($this->a = = = $this->b) {//End Quote
- Break
- }
- if (Ord ($this->a) <= self::ord_lf) {
- throw New Jsmin_unterminatedstringexception (
- "Jsmin:unterminated String at Byte"
- . $this->inputindex. ": {$str}");
- }
- $str. = $this->a;
- if ($this->a = = = ' \ \ ') {
- $this->output. = $this->a;
- $this->a = $this->get ();
- $str. = $this->a;
- }
- }
- }
- Fallthrough
- Case Self::action_delete_a_b:
- $this->b = $this->next ();
- if ($this->b = = = '/' && $this->isregexpliteral ()) {//REGEXP literal
- $this->output. = $this->a. $this->b;
- $pattern = '/'; In case needed for exception
- while (true) {
- $this->a = $this->get ();
- $pattern. = $this->a;
- if ($this->a = = = = '/') {//End pattern
- Break while (true)
- } elseif ($this->a = = = ' \ \ ') {
- $this->output. = $this->a;
- $this->a = $this->get ();
- $pattern. = $this->a;
- } elseif (Ord ($this->a) <= self::ord_lf) {
- throw New Jsmin_unterminatedregexpexception (
- "Jsmin:unterminated RegExp at Byte"
- . $this->inputindex. ": {$pattern}");
- }
- $this->output. = $this->a;
- }
- $this->b = $this->next ();
- }
- End Case Action_delete_a_b
- }
- }
- protected function isregexpliteral ()
- {
- if (False!== Strpos ("\n{;(, =:[!&|?", $this->a)) {//we aren ' t dividing
- return true;
- }
- if (' = = = $this->a) {
- $length = strlen ($this->output);
- if ($length < 2) {//weird edge case
- return true;
- }
- You can ' t divide a keyword
- if (Preg_match ('/(?: case|else|in|return|typeof) $/', $this->output, $m)) {
- if ($this->output = = = $m [0]) {//odd but could happen
- return true;
- }
- Make sure it's a keyword, not end of a identifier
- $charBeforeKeyword = substr ($this->output, $length-strlen ($m [0])-1, 1);
- if (! $this->isalphanum ($charBeforeKeyword)) {
- return true;
- }
- }
- }
- return false;
- }
- /**
- * Get next char. Convert Ctrl char to space.
- */
- 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 {
- return null;
- }
- }
- if ($c = = = "\ r" | | $c = = = "\ n") {
- return "\ n";
- }
- if (Ord ($c) < Self::ord_space) {//control Char
- Return ';
- }
- return $c;
- }
- /**
- * Get next char. If is the CTRL character, translate to a space or newline.
- */
- protected function Peek ()
- {
- $this->lookahead = $this->get ();
- return $this->lookahead;
- }
- /**
- * is $c a letter, digit, underscore, dollar sign, escape, or Non-ascii?
- */
- protected function Isalphanum ($c)
- {
- Return (Preg_match ('/^[0-9a-za-z_\\$\\\\]$/', $c) | | ord ($c) > 126);
- }
- protected function singlelinecomment ()
- {
- $comment = ";
- while (true) {
- $get = $this->get ();
- $comment. = $get;
- if (Ord ($get) <= self::ord_lf) {//EOL reached
- If IE conditional comment
- if (Preg_match ('/^\\/@ (?: cc_on|if|elif|else|end) \\b/', $comment)) {
- Return "/{$comment}";
- }
- return $get;
- }
- }
- }
- protected function multiplelinecomment ()
- {
- $this->get ();
- $comment = ";
- while (true) {
- $get = $this->get ();
- if ($get = = = ' * ') {
- if ($this->peek () = = = '/') {//end of comment reached
- $this->get ();
- If comment preserved by YUI Compressor
- if (0 = = Strpos ($comment, '! ')) {
- Return "\n/*". SUBSTR ($comment, 1). "*/\n";
- }
- If IE conditional comment
- if (Preg_match ('/^@ (?: cc_on|if|elif|else|end) \\b/', $comment)) {
- Return "/*{$comment}*/";
- }
- Return ';
- }
- } elseif ($get = = = null) {
- throw New Jsmin_unterminatedcommentexception (
- "Jsmin:unterminated Comment at byte"
- . $this->inputindex. ":/*{$comment}");
- }
- $comment. = $get;
- }
- }
- /**
- * Get the next character, skipping over comments.
- * Some comments may preserved.
- */
- protected function Next ()
- {
- $get = $this->get ();
- if ($get!== '/') {
- return $get;
- }
- Switch ($this->peek ()) {
- Case '/': Return $this->singlelinecomment ();
- Case ' * ': Return $this->multiplelinecomment ();
- Default:return $get;
- }
- }
- }
- Class Jsmin_unterminatedstringexception extends Exception {}
- Class Jsmin_unterminatedcommentexception extends Exception {}
- Class Jsmin_unterminatedregexpexception extends Exception {}
- ?>
Copy CodeInvocation Example:
Copy Code |