• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

phpjscss加载合并函数宋正河整理

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
<?php
//php js css加载合并函数 宋正河整理
//转载请注明出处
define('COMBINE_JS',true);
define('COMBINE_CSS',true);
 
define('APPLICATION_JS_ROOT','E:/wwwroot/m/js/');
define('APPLICATION_CSS_ROOT','E:/wwwroot/m/css/');
 
define('SC_ROOT','E:/wwwroot/m/static_cache/');
define('SC_URL','http://www.localhost.com/m/static_cache/');
 
define('APPLICATION_JS','http://www.localhost.com/m/js/');
define('APPLICATION_CSS','http://www.localhost.com/m/css/');
 
function load_combine_js($js,$path=array(),$origin=false,$part=1){
    if(!is_array($js)) return false;
    if(empty($path)){
        $path_url=APPLICATION_JS;
        $path_root=APPLICATION_JS_ROOT;
    }
    if($origin or (!COMBINE_JS)){
        $string='';
        for($i=0;$i<count($js);$i++){$string.="<script src='{$path_url}{$js[$i]}.js'></script>\n";}
        echo $string;
        return true;
    }
    $cache_filename='js-'.md5($_SERVER['SCRIPT_NAME'].$part).'.js';
    if(!file_exists(SC_ROOT.$cache_filename)){
        $file_content='';
        for($i=0;$i<count($js);$i++){
            $f=$path_root.$js[$i].'.js';
            if(file_exists($f)) $file_content.=file_get_contents($f)."\n";
        }
        file_put_contents(SC_ROOT.$cache_filename,$file_content);
    }
    $file=SC_URL.$cache_filename;
    echo "<script src='{$file}'></script>\n";
    return true;
}
 
function load_combine_css($css,$path=array(),$origin=false,$part=1){
    if(!is_array($css)) return false;
    if(empty($path)){
        $path_url=APPLICATION_CSS;
        $path_root=APPLICATION_CSS_ROOT;
    }
    if($origin or (!COMBINE_CSS)){
        $string='';
        for($i=0;$i<count($css);$i++){$string.="<link href='{$path_url}{$css[$i]}.css' rel='stylesheet' type='text/css' />\n";}
        echo $string;
        return true;
    }
    $cache_filename='css-'.md5($_SERVER['SCRIPT_NAME'].$part).'.css';
    if(!file_exists(SC_ROOT.$cache_filename)){
        $file_content='';
        for($i=0;$i<count($css);$i++){
            $f=$path_root.$css[$i].'.css';
            if(file_exists($f)) $file_content.=file_get_contents($f)."\n";
        }
        file_put_contents(SC_ROOT.$cache_filename,$file_content);
    }
    $file=SC_URL.$cache_filename;
    echo $string="<link href='{$file}' rel='stylesheet' type='text/css' />\n";
    return true;
}
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<?php
load_combine_js(array('a','b'),array(),true);
load_combine_js(array('a','b'));
load_combine_css(array('1','2'),array(),true);
load_combine_css(array('1','2'));
?>
</head>
<body>
123
</body>
</html>

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHP AMP_get_renderer函数代码示例发布时间:2022-05-24
下一篇:
JavaScript,Java,php的区分大小写问题发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap