如何使用php脚本给html中引用的js和css路径打上版本号
在搜索引擎中搜索关键字.htaccess缓存,你可以搜索到很多关于设置网站文件缓存的教程,通过设置可以将css、js等不太经常更新的文件缓存在浏览器端,这样访客每次访问你的网站的时候,浏览器就可以从浏览器的缓存中获取css、js等,而不必从你的服务器读取,这样在一定程度上加快了网站的打开速度,又可以节约一下你的服务器流量。
具体文字说明不给大家多说了,下面通过代码实例给大家讲解。
比如
<linkrel="stylesheet"type="text/css"href="./css/globel.css"> <scriptsrc="./js/config.js"></script>
中的href和src加上版本
<linkrel="stylesheet"type="text/css"href="./css/globel.css?eslc-app=3-0-2"> <scriptsrc="./js/config.js?eslc-app=3-0-2"></script>
当然如果不是前后端分离得干干净净的,就没必要这么额外的这样自己在写个脚本去打版本。
打版本的好处:
解决外部引用文件实时更新问题。比如
pc端上主要体现在iframe中的外部引用文件不会实时更新。
wap端上部分app也是比如微信。如果你的网页是嵌到自己的app,那也更不用说了。
用php写了个类
//生成版本
//清除版本
classReplaceVersion{
protected$filePostFixs=array();
protected$versionName=null;
protected$version=null;
protected$path=null;
/**
*@parammixed$configs
*@param[type]$profix[description]
*@param[type]$path[description]
*/
publicfunction__construct($configs,$profix,$path){
if(!$this->isCanRun()){
$this->error('必须在内网环境10.10.0开头才可运行');//exit;
}
$this->setVersion($configs);
$this->setFilePostFix($profix);
$this->path=$path;
}
protectedfunctionisCanRun(){
if(strpos($_SERVER['HTTP_HOST'],'10.10.0')!==false){
returntrue;
}
returnfalse;
}
/**
*匹配到script节点
*@paramarray$match匹配到的script
*@returnstring处理好的script
*/
protectedfunctioncallbackScript($match){
//["<scriptsrc="../js/config.js?is=new"></script>","../js/config.js","?is=new"]
/*/<script.*?src=\"(.*?)(\?.*?|\?)?\".*?><\/script>/*/
$str=$match[0];
$pattern='/(<script.*?src=\")(.*)?(\"><\/script>)/';
return$this->callbackMatch($str,$pattern);
}
/**
*匹配到css节点
*@paramarray$match匹配到的css
*@returnstring处理好的css
*/
protectedfunctioncallbackCss($match){
//'<linkrel="stylesheet"type="text/css"href="../css/globel.css">';
$str=$match[0];
$pattern='/(<link.*?href=\")(.*)?(\".*?>)/';
return$this->callbackMatch($str,$pattern);
}
/**
*回调模式匹配
*@paramstring$str
*@paramstring$pattern
*@returnstring
*/
protectedfunctioncallbackMatch($str,$pattern){
switch($this->dealFlag){
case'replace':
return$this->replaceCallbackMatch($str,$pattern);
case'clean':
return$this->cleanCallbackMatch($str,$pattern);
default:
$this->error('非法模式');
}
}
/**
*替换版本
*@paramstring$str待处理的string
*@paramstring$pattern正则
*@returnstring处理后的string
*/
protectedfunctionreplaceCallbackMatch($str,$pattern){
if(!preg_match($pattern,$str,$third)){
return$str;
}
$arr=explode('?',$third[2]);
$len=count($arr);
$versionName=$this->versionName;
$version=$this->version;
if($len===1){//没有问号
$arr[0].='?'.$versionName.'='.$version;
}else{//有问号
if(preg_match('/(^|\&)'.$versionName.'=(.*?)($|\&)/',$arr[1])){//存在
$arr[1]=preg_replace('/(^|\&)'.$versionName.'=(.*?)($|\&)/','$1'.$versionName.'='.$version.'$3',$arr[1]);
$arr[0].='?'.$arr[1];
}else{//不存在
$arr[0].='?'.$arr[1].'&'.$versionName.'='.$version;
}
}
return$third[1].$arr[0].$third[3];
}
/**
*清除版本
*@paramstring$str待清除的版本
*@paramstring$pattern正则
*@returnstring清除后的string
*/
protectedfunctioncleanCallbackMatch($str,$pattern){
if(!preg_match($pattern,$str,$third)){
return$str;
}
$arr=explode('?',$third[2]);
$len=count($arr);
$versionName=$this->versionName;
if($len>1&&strpos($arr[1],$versionName.'=')!==false){
$arr[1]=preg_replace('/(^|\&)'.$versionName.'=(.*?)($|\&)/','$1',$arr[1]);
substr($arr[1],-1)==='&'&&($arr[1]=substr($arr[1],0,-1));
$arr[0].=strlen($arr[1])>0?'?'.$arr[1]:'';
$str=$third[1].$arr[0].$third[3];
}
return$str;
}
/**
*执行
*/
protectedfunctionrun(){
if($this->path==''){
$this->error('emptypath');
return;
}
if(is_dir($this->path)){
$this->setDirFilesVersion($this->path);
}elseif(is_file($this->path)){
$this->setFileVersion($this->path);
}else{
$this->error('errorpath');
}
}
/**
*添加版本
*/
publicfunctionreplace(){
$this->dealFlag='replace';
$this->run();
echo'replacesuccess';
}
/**
*清除版本
*/
publicfunctionclean(){
$this->dealFlag='clean';
$this->run();
echo'cleansuccess';
}
protectedfunctionsuccess(){
}
protectedfunctionerror($errorMsg){
echo$errorMsg;
exit();
}
protectedfunctionsetDirFilesVersion($dir){
$handle=null;
$file=null;
if($handle=opendir($dir)){
while(false!==($file=readdir($handle))){
if($file==='.'||$file==='..'||strpos($file,'.')===-1){continue;}
$this->setFileVersion($file);
}
}
}
protectedfunctionsetFileVersion($file){
$temp=null;
/*$pattern='/<script.*?src=\"(.*?)(\?.*?|\?)?\".*?><\/script>/';*/
$temp=explode('.',$file);
if(!$this->isNeedReplacePostFix(array_pop($temp))){return;}
$content=null;
$content=file_get_contents($file);
$content=preg_replace_callback('/<script.*?><\/script>/',array(&$this,'callbackScript'),$content);
$content=preg_replace_callback('/<link.*?type="text\/css".*?>/',array(&$this,'callbackCss'),$content);
//highlight_string($content);
file_put_contents($file,$content);
}
/**
*获得版本
*@parammixed$configsarray('versionName':'version')||$versionName
*/
protectedfunctionsetVersion($configs){
//last_wap_version='3-0-0',
//wap_version='3-0-1',
if(is_array($configs)&&$configs>0){
foreach($configsas$key=>$value){
$this->version=$value;
$this->versionName=$key;
}
}elseif(is_string($configs)&&$configs!=''){
$configs=explode(',',$configs);
$this->versionName=$configs[0];
count($configs)==2&&($this->version=$configs[1]);
}else{
$this->error('theversionisempty');
}
}
/**
*通过后缀判断该文件是否要添加或清除版本
*@paramstring$profix后缀
*@returnbooleantrue|false
*/
protectedfunctionisNeedReplacePostFix($profix){
if(in_array($profix,$this->filePostFixs)){
returntrue;
}
returnfalse;
}
/**
*设置需要操作的后缀
*/
publicfunctionsetFilePostFix($profix){
if(is_array($profix)){
count($profix)>0&&($this->filePostFixs=array_merge($this->filePostFixs,$profix));
}elseif(is_string($profix)){
$this->filePostFixs[]=$profix;
}
}
}
使用:
$dir=__DIR__;
$is_clean=false;
//$is_clean=true;
//第一个参就是版本信息,第二个就是要匹配的文件后缀,第三个是要匹配的目录或者文件
if($is_clean){//清除版本
$configs='eslc-wap';
$replaceObj=newReplaceVersion($configs,array('html'),$dir);
$replaceObj->clean();
}else{//添加或替换版本
$configs=array('eslc-wap'=>'1.0.1');//也可以写成$configs='eslc-wap,1.0.1';
$replaceObj=newReplaceVersion($configs,array('html'),$dir);
$replaceObj->replace();
}热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短