≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > PHP笔记

php 给html中引用的js和css路径打上版本号的实现方法

人气:24 时间:2022-12-23

这篇文章主要为大家详细介绍了php 给html中引用的js和css路径打上版本号的实现方法,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小玲来看看吧!

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

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

比如

代码如下:

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

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

中的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>

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

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

打版本的好处:

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

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

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

用php写了个类

代码如下:
 

/**
 * php给html中引用的js和css路径打上版本号
 * @param 
 * @arrange (512.笔记) www.q1010.com
 **/
//生成版本
//清除版本
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;
  }
 }
}

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

使用:

代码如下:

/**
 * php给html中引用的js和css路径打上版本号
 * @param 
 * @arrange (512.笔记) www.q1010.com
 **/
$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脚本给html中引用的js和css路径打上版本号

 

本文来自:http://www.q1010.com/173/22660-0.html

注:关于php 给html中引用的js和css路径打上版本号的实现方法的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:路径

您可能感兴趣的文章

  • PHP循环输出指定目录下的所有文件和文件夹路径的简单示例
  • PHP统计nginx访问日志中的搜索引擎抓取404链接页面路径的简单示例
  • php求两个目录的相对路径的简单示例
  • php获取文件夹路径内的图片及分页显示的简单示例
  • MySQL中查看数据库安装路径的简单示例
  • python3实现Dijkstra算法最短路径的简单示例
  • windows文件系统机制引发的PHP路径爆破问题的深入分析
  • php上传图片之时间戳命名(保存路径)的简单示例
  • ThinkPHP中URL路径访问与模块控制器之间的关系的简单示例
  • ThinkPHP中pathinfo的访问模式、路径访问模式及URL重写的简单示例
上一篇:php 简单的上传进度条的简单示例
下一篇:php 实现计算年龄精准到年月日的实例
热门文章
  • PHP 写入WRITE编码为UTF8的文件示例
  • PHP 中文字符串截取函数示例:支持gb2312,gbk,big
  • PHP 简单留言板的制作示例
  • 解决Fatal error: Call to undefined function mb_convert_encoding() in错误问题
  • PHP语言基础(标记、注释、变量、数组、常量、函数)示例
  • php 生成迅雷链接的简单示例
  • php 获取短网址的实现方法
  • PHP 通用分页类的简单示例
  • PHP 使用文件方式导入导出整个MYSQL数据库的实现方法
  • php 获取MYSQL错误的简单示例
  • 最新文章
    • 解决PHP使用redis实现统计缓存MySQL压力的问题
    • php 简单的上传进度条的简单示例
    • php 给html中引用的js和css路径打上版本号的实现方法
    • php 实现计算年龄精准到年月日的实例
    • php+ajax无刷新分页的简单示例
    • 解决php+ajax无刷新上传图片的问题
    • 解决PHP生成HTML静态页面的问题
    • 解决PHP使用uniqid函数生成唯一ID的问题
    • 解决PHP防刷票的一些问题
    • 微信access_token的获取开发的实现方法

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。