日期:2014-05-17 浏览次数:20570 次
1. 压缩css
?
compress.php
?
<?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(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer); return $buffer; } /* your css files */ include('galleria.css'); include('articles.css'); ob_end_flush();?
实例化:
?
test.php
?
<link href="compress.php" rel="stylesheet" type="text/css" /> <span id="tester">test</span>?
?
2. 压缩js
?
利用jsmin类
?
来源: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'));?
common.js
?
alert('first js');?
common.js
?
alert('second js');?
jsmin.php
?
<?php /** * jsmin.php - extended PHP implementation of Douglas Crockford's JSMin. * * <code> * $minifiedJs = JSMin::minify($js); * </code> * * This is 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 are preserved, as are multi-line * comments that begin with "/*!" (for documentation purposes). In the latter case * newlines are 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 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, publish, distribute, sublicense, and/or sell copies * of the Software, and to permit persons to whom the Software is furnished to do * so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * The Software shall be 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 AN 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 <ryan@wonko.com> (PHP port) * @author Steve Clay <steve@mrclay.org> (modifications + cleanup) * @author Andrea Giammarchi <http://www.3site.eu> (spaceBeforeRegExp) * @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c) * @copyright 2008 Ryan Grove <ryan@wonko.com> (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