如何使用php脚本给html中引用的js和css路径打上版本号


Posted in PHP onNovember 18, 2015

在搜索引擎中搜索关键字.htaccess 缓存,你可以搜索到很多关于设置网站文件缓存的教程,通过设置可以将css、js等不太经常更新的文件缓存在浏览器端,这样访客每次访问你的网站的时候,浏览器就可以从浏览器的缓存中获取css、js等,而不必从你的服务器读取,这样在一定程度上加快了网站的打开速度,又可以节约一下你的服务器流量。

具体文字说明不给大家多说了,下面通过代码实例给大家讲解。

比如

<link rel="stylesheet" type="text/css" href="./css/globel.css">
<script src="./js/config.js"></script>

中的href和src加上版本

<link rel="stylesheet" type="text/css" href="./css/globel.css?eslc-app=3-0-2">
<script src="./js/config.js?eslc-app=3-0-2"></script>

当然如果不是前后端 分离得干干净净的,就没必要这么额外的这样自己在写个脚本去打版本。

打版本的好处:

解决外部引用文件实时更新问题。比如

pc端上主要体现在 iframe中的外部引用文件不会实时更新。

wap端上部分app也是比如微信。 如果你的网页是嵌到自己的app,那也更不用说了。

用php写了个类

//生成版本
//清除版本
class ReplaceVersion{
 protected $filePostFixs = array();
 protected $versionName = null;
 protected $version = null;
 protected $path = null;
 /**
  * @param mixed $configs 
  * @param [type] $profix [description]
  * @param [type] $path  [description]
  */
 public function __construct($configs, $profix, $path){
  if (!$this->isCanRun()) {
   $this->error('必须在内网环境 10.10.0开头才可运行'); //exit;
  }
  $this->setVersion($configs);
  $this->setFilePostFix($profix);
  $this->path = $path;
 }
 protected function isCanRun(){
  if (strpos($_SERVER['HTTP_HOST'], '10.10.0') !== false) {
   return true;
  }
  return false;
 }
 /**
  * 匹配到script节点
  * @param array $match 匹配到的script
  * @return string 处理好的script
  */
 protected function callbackScript($match){
  //["<script src="../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节点
  * @param array $match 匹配到的css
  * @return string 处理好的css
  */
 protected function callbackCss($match){
  // '<link rel="stylesheet" type="text/css" href="../css/globel.css">';
  $str = $match[0];
  $pattern = '/(<link.*?href=\")(.*)?(\".*?>)/';
  return $this->callbackMatch($str, $pattern);
 }
 /**
  * 回调模式匹配
  * @param string $str 
  * @param string $pattern
  * @return string  
  */
 protected function callbackMatch($str, $pattern){
  switch ($this->dealFlag) {
   case 'replace':
    return $this->replaceCallbackMatch($str, $pattern);
   case 'clean':
    return $this->cleanCallbackMatch($str, $pattern);
   default:
    $this->error('非法模式');
  }
 }
 /**
  * 替换版本
  * @param string $str 待处理的string
  * @param string $pattern 正则
  * @return string  处理后的string
  */
 protected function replaceCallbackMatch($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];
 }
 /**
  * 清除版本
  * @param string $str 待清除的版本
  * @param string $pattern 正则
  * @return string  清除后的string
  */
 protected function cleanCallbackMatch($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;
 }
 /**
  * 执行
  */
 protected function run(){
  if ($this->path == '') {
   $this->error('empty path');
   return ;
  }
  if (is_dir($this->path)) {
   $this->setDirFilesVersion( $this->path );
  }else if(is_file($this->path)){
   $this->setFileVersion( $this->path );
  }else{
   $this->error('error path');
  }
 }
 /**
  * 添加版本
  */
 public function replace(){
  $this->dealFlag = 'replace';
  $this->run();
  echo 'replace success';
 }
 /**
  * 清除版本
  */
 public function clean(){
  $this->dealFlag = 'clean';
  $this->run();
  echo 'clean success';
 }
 protected function success(){
 }
 protected function error($errorMsg){
  echo $errorMsg;
  exit();
 }
 protected function setDirFilesVersion($dir){
  $handle = null;
  $file  = null;
  if ( $handle = opendir($dir)) {
   while ( false !== ($file = readdir($handle)) ) {
    if ($file === '.' || $file === '..' || strpos($file, '.') === -1 ) {continue;}
    $this->setFileVersion($file);
   }
  }
 }
 protected function setFileVersion($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);
 }
 /**
  * 获得版本
  * @param mixed $configs array( 'versionName' : 'version') || $versionName
  */
 protected function setVersion($configs){
  // last_wap_version  = '3-0-0', 
  // wap_version = '3-0-1',
  if (is_array($configs) && $configs > 0) {
   foreach ($configs as $key => $value) {
    $this->version = $value;
    $this->versionName = $key;
   }
  }else if(is_string($configs) && $configs != ''){
   $configs = explode(',', $configs);
   $this->versionName = $configs[0];
   count($configs) == 2 && ($this->version = $configs[1]);
  }else{
   $this->error('the version is empty');
  }
 }
 /**
  * 通过后缀判断该文件是否要添加或清除版本
  * @param string $profix 后缀
  * @return boolean  true | false
  */
 protected function isNeedReplacePostFix($profix){
  if (in_array($profix, $this->filePostFixs)) {
   return true;
  }
  return false;
 }
 /**
  * 设置需要操作的后缀
  */
 public function setFilePostFix($profix){
  if (is_array($profix)) {
   count($profix) > 0 && ( $this->filePostFixs = array_merge($this->filePostFixs, $profix) );
  }else if(is_string($profix)){
   $this->filePostFixs[] = $profix;
  }
 }
}

使用:

$dir  = __DIR__;
$is_clean = false;
//$is_clean = true;
//第一个参就是版本信息, 第二个就是要匹配的文件后缀, 第三个是要匹配的目录或者文件
if ($is_clean) {//清除版本
 $configs = 'eslc-wap';
 $replaceObj = new ReplaceVersion($configs, array('html'), $dir);
 $replaceObj->clean();
}else{//添加或替换版本
 $configs = array('eslc-wap' => '1.0.1');//也可以写成 $configs = 'eslc-wap, 1.0.1';
 $replaceObj = new ReplaceVersion($configs, array('html'), $dir);
 $replaceObj->replace();
}
PHP 相关文章推荐
一个PHP+MSSQL分页的例子
Oct 09 PHP
用php+mysql一个名片库程序
Oct 09 PHP
PHP sprintf()函数用例解析
May 18 PHP
php在文件指定行中写入代码的方法
May 23 PHP
关于PHP二进制流 逐bit的低位在前算法(详解)
Jun 13 PHP
Memcached常用命令以及使用说明详解
Jun 27 PHP
ThinkPHP 404页面的设置方法
Jan 14 PHP
PHP的cURL库简介及使用示例
Feb 06 PHP
php中分页及SqlHelper类用法实例
Jan 12 PHP
php如何实现不借助IDE快速定位行数或者方法定义的文件和位置
Jan 17 PHP
Thinkphp 框架基础之源码获取、环境要求与目录结构分析
Apr 27 PHP
PHP使用非对称加密算法RSA
Apr 21 PHP
php生成唯一数字id的方法汇总
Nov 18 #PHP
基于PHP给大家讲解防刷票的一些技巧
Nov 18 #PHP
使用PHP uniqid函数生成唯一ID
Nov 18 #PHP
使用PHP实现生成HTML静态页面
Nov 18 #PHP
php+ajax无刷新上传图片实例代码
Nov 17 #PHP
php计算年龄精准到年月日
Nov 17 #PHP
php实现简单的上传进度条
Nov 17 #PHP
You might like
PHP新手上路(十二)
2006/10/09 PHP
php恢复数组的key为数字序列的方法
2015/04/28 PHP
Laravel5.7 数据库操作迁移的实现方法
2019/04/12 PHP
js控制frameSet示例
2013/09/10 Javascript
JavaScript中number转换成string介绍
2014/12/31 Javascript
JavaScript控制网页层收起和展开效果的方法
2015/04/15 Javascript
js滑动提示效果代码分享
2016/03/10 Javascript
底部悬浮通栏可以关闭广告位的实现方法
2016/06/01 Javascript
简单分析javascript中的函数
2016/09/10 Javascript
JS求解三元一次方程组值的方法
2017/01/03 Javascript
layer弹出层中H5播放器全屏出错的解决方法
2017/02/21 Javascript
jQuery实现字符串全部替换的方法【推荐】
2017/03/09 Javascript
vue实现配置全局访问路径头(axios)
2019/11/01 Javascript
Vue实现导航栏菜单
2020/08/19 Javascript
[04:14]从西雅图到上海——玩家自制DOTA2主题歌曲应援TI9
2019/07/11 DOTA
[01:03:50]DOTA2-DPC中国联赛 正赛 CDEC vs DLG BO3 第二场 2月7日
2021/03/11 DOTA
推荐11个实用Python库
2015/01/23 Python
python杀死一个线程的方法
2015/09/06 Python
在Python中给Nan值更改为0的方法
2018/10/30 Python
[原创]Python入门教程5. 字典基本操作【定义、运算、常用函数】
2018/11/01 Python
对python中dict和json的区别详解
2018/12/18 Python
python+ffmpeg批量去视频开头的方法
2019/01/09 Python
python使用tomorrow实现多线程的例子
2019/07/20 Python
Pytorch中实现只导入部分模型参数的方式
2020/01/02 Python
Python读取文件内容为字符串的方法(多种方法详解)
2020/03/04 Python
python中adb有什么功能
2020/06/07 Python
python Xpath语法的使用
2020/11/26 Python
Python 找出英文单词列表(list)中最长单词链
2020/12/14 Python
可以在一个PHP文件里面include另外一个PHP文件两次吗
2015/05/22 面试题
应届生文秘专业个人自荐信格式
2013/09/21 职场文书
cf战队收人广告词
2014/03/14 职场文书
勤俭节约倡议书
2014/04/14 职场文书
奥巴马连任演讲稿
2014/05/15 职场文书
家属联谊会致辞
2015/07/31 职场文书
sql注入教程之类型以及提交注入
2021/08/02 MySQL
JS轻量级函数式编程实现XDM三
2022/06/16 Javascript