您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

压缩多个CSS与JS文件的php代码

2024/3/15 13:49:31发布24次查看
复制代码
实例化:test.php
test
复制代码
2. 压缩js利用jsmin类来源:http://code.google.com/p/minify/ compress.php
复制代码
common.jsalert('first js');
common.jsalert('second js');
jsmin.php
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 will 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) 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) 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 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 an 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 inputlength) { $c = $this->input[$this->inputindex]; $this->inputindex += 1; } else { return null; } } if ($c === \r || $c === \n) { return \n; } if (ord($c) return ' '; } return $c; } /** * get next char. if is 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) // 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 be 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 {} ?>
复制代码
调用示例:
复制代码
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product