教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP压缩javascritp 与CSS的例子

PHP压缩javascritp 与CSS的例子

发布时间:2016-10-26   编辑:jiaochengji.com
教程集为您提供PHP压缩javascritp 与CSS的例子等资源,欢迎您收藏本站,我们将为您提供最新的PHP压缩javascritp 与CSS的例子资源
有的网站有很多的 CSS 文件,如果将它们合并到一起并且进行 Gzip 压缩会减少请求和文件大小,有利于提高网站加载速度。为了方便我不推荐人工压缩和合并 CSS,而是使用 PHP 代码。

首先将所有 CSS 放到一个目录里,然后在此目录新建一个空的 CSS 文件,命名为 css.php(其实除了后缀命名随便)。

然后在 PHP 文件里放下边的代码:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5688')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5688>

<?php
header('Content-type: text/css');
ob_start("compress");
function compress($buffer) {
 $buffer = preg_replace('!/*[^*]** ([^/][^*]** )*/!', '', $buffer);
 $buffer = str_replace(array("rn", "r", "n", "t", '  ', '    ', '    '), '', $buffer);
 return $buffer;
}
/*include('colorpicker.css');
include('jquery-ui.css');
include('style.css');
include('switchery.min.css');*/
foreach( glob( "*.css" ) as $filename ) include $filename;
ob_end_flush();

引入 CSS 文件的代码换成引入这个 PHP 文件,例如:


<link rel='stylesheet' id='style-css'  href='/css/css.php' type='text/css' media='all' />


压缩多个css为一个css

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3466')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3466>

<?php
/*
Compress multiple CSS files into one and cache for an hour.

Use the same code for Javascript, but replace below "text/css" with "text/javascript" and of course make sure you include .js files instead of .css ones.
*/  www.jiaochengji.com
ob_start("ob_gzhandler");
header("Content-type: text/css; charset: UTF-8");    
header("Expires: ".gmdate("D, d M Y H:i:s", time() 60*60)." GMT");

include('somefile.css');
echo "nn";
include('anotherfile.css');
echo "nn";

ob_flush();


下面整理可压缩css,js的函数

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8263')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8263>


/**
 * 合并css样式为一个文件
 *
 * @param unknown_type $urls
 * @param unknown_type $path
 * @param unknown_type $tmpl_path
 * @return unknown
 */
function parse_css($urls,$path="./static/",$tmpl_path='.'){
    $url = md5(implode(',',$urls));
    $css_url = $path.$url.'.css';
    if(!file_exists($css_url)){
        if(!file_exists($path))mkdir($path,0777);
        $css_content = '';
        foreach($urls as $url){
         $css_content .= @file_get_contents($url);
        }
        $css_content = preg_replace("/[rn]/",'',$css_content);
        $css_content = str_replace("../images/",$tmpl_path."/images/",$css_content);
        @file_put_contents($css_url,$css_content);
    }
    return $css_url;
}
/**
 * 合并js www.jiaochengji.com为一个文件
 *
 * @param unknown_type $urls
 * @param unknown_type $path
 * @return unknown
 */
function parse_script($urls,$path="./static/"){
    $url = md5(implode(',',$urls));
    $js_url = $path.$url.'.js';
    if(!file_exists($js_url))
    {
        if(!file_exists($path))mkdir($path,0777);
        require_once "inc/javascriptpacker.php";
        $js_content = '';
        foreach($urls as $url)
        {
            $append_content = @file_get_contents($url)."rn";
            $packer = new JavaScriptPacker($append_content);
            $append_content = $packer->pack();
            $js_content .= $append_content;
        }
        @file_put_contents($js_url,$js_content);
    }
    return $js_url;
}
前台
js调用
<?php
$pagejs[] = $tplurl."js/jump.js";
$jsfile=parse_script($pagejs,"./template/default/js/",".");
?>
<script type="text/javascript" src="<?=$jsfile?>"></script>
css调用
<?php
$pagecss[] = $tplurl."style/index_top.css";
$pagecss[] = $tplurl."style/index.css";
$cssfile=parse_css($pagecss,"./template/default/style/",".");
?>
<link rel="stylesheet" type="text/css" href="<?=$cssfile?>" />

PHP 文件里就包含了所有被压缩的 CSS 代码,而且可以自动引入 CSS 目录里的所有 CSS 文件,不用在新建 CSS 文件的时候再修改这个 PHP 文件。

您可能感兴趣的文章:
PHP压缩javascritp 与CSS的例子
PHP开启gzip压缩的二种方法
php 压缩多个CSS文件的实现代码
php压缩与解压缩类PclZip的例子
php gzip压缩js与css代码的例子
php gzip页面压缩的两个例子
php 字符串压缩的例子
php压缩函数(gzcompress gzuncompress)压缩字符串
PHP压缩解压缩类PclZip用法教程
php 字符串压缩方法对比分析

[关闭]
~ ~