Rumah pembangunan bahagian belakang tutorial php 如何使用php脚本给html中引用的js和css路径打上版本号_php实例

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

Jun 07, 2016 pm 05:10 PM
css html js skrip php Versi laluan

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

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

比如

1

2

<link rel="stylesheet" type="text/css" href="./css/globel.css">

<script src="./js/config.js"></script>

Salin selepas log masuk

中的href和src加上版本

1

2

<link rel="stylesheet" type="text/css" href="./css/globel.css&#63;eslc-app=3-0-2">

<script src="./js/config.js&#63;eslc-app=3-0-2"></script>

Salin selepas log masuk

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

打版本的好处:

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

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

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

用php写了个类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

//生成版本

//清除版本

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&#63;is=new"></script>", "../js/config.js", "&#63;is=new"]

  /*/<script.*&#63;src=\"(.*&#63;)(\&#63;.*&#63;|\&#63;)&#63;\".*&#63;><\/script>/*/

  $str = $match[0];

  $pattern = '/(<script.*&#63;src=\")(.*)&#63;(\"><\/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.*&#63;href=\")(.*)&#63;(\".*&#63;>)/';

  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('&#63;', $third[2]);

  $len  = count($arr);

  $versionName = $this->versionName;

  $version = $this->version;

  if ($len === 1) {//没有问号

   $arr[0] .= '&#63;'. $versionName. '='. $version;

  }else{//有问号

   if (preg_match('/(^|\&)'. $versionName.'=(.*&#63;)($|\&)/', $arr[1])) {//存在

    $arr[1] = preg_replace('/(^|\&)'. $versionName.'=(.*&#63;)($|\&)/', '$1'. $versionName.'='. $version. '$3', $arr[1]);

    $arr[0] .= '&#63;'. $arr[1];

   }else{//不存在

    $arr[0] .= '&#63;'. $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('&#63;', $third[2]);

  $len  = count($arr);

  $versionName = $this->versionName;

  if ($len > 1 && strpos($arr[1], $versionName. '=') !== false) {

   $arr[1] = preg_replace('/(^|\&)'. $versionName.'=(.*&#63;)($|\&)/', '$1', $arr[1]);

   substr($arr[1], -1) === '&' && ($arr[1] = substr($arr[1], 0, -1));

   $arr[0] .= strlen($arr[1]) > 0 &#63; '&#63;'. $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.*&#63;src=\"(.*&#63;)(\&#63;.*&#63;|\&#63;)&#63;\".*&#63;><\/script>/';*/

  $temp = explode('.', $file) ;

  if ( ! $this->isNeedReplacePostFix(array_pop( $temp )) ) {return;}

  $content = null;

  $content = file_get_contents($file);

  $content = preg_replace_callback('/<script.*&#63;><\/script>/', array(&$this, 'callbackScript'), $content);

  $content = preg_replace_callback('/<link.*&#63;type="text\/css".*&#63;>/', 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;

  }

 }

}

Salin selepas log masuk

使用:

1

2

3

4

5

6

7

8

9

10

11

12

13

$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();

}

Salin selepas log masuk

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Tag artikel panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Jadual Bersarang dalam HTML Jadual Bersarang dalam HTML Sep 04, 2024 pm 04:49 PM

Jadual Bersarang dalam HTML

Sempadan Jadual dalam HTML Sempadan Jadual dalam HTML Sep 04, 2024 pm 04:49 PM

Sempadan Jadual dalam HTML

HTML jidar-kiri HTML jidar-kiri Sep 04, 2024 pm 04:48 PM

HTML jidar-kiri

Susun Atur Jadual HTML Susun Atur Jadual HTML Sep 04, 2024 pm 04:54 PM

Susun Atur Jadual HTML

Memindahkan Teks dalam HTML Memindahkan Teks dalam HTML Sep 04, 2024 pm 04:45 PM

Memindahkan Teks dalam HTML

Senarai Tertib HTML Senarai Tertib HTML Sep 04, 2024 pm 04:43 PM

Senarai Tertib HTML

Bagaimana anda menghuraikan dan memproses HTML/XML dalam PHP? Bagaimana anda menghuraikan dan memproses HTML/XML dalam PHP? Feb 07, 2025 am 11:57 AM

Bagaimana anda menghuraikan dan memproses HTML/XML dalam PHP?

Butang onclick HTML Butang onclick HTML Sep 04, 2024 pm 04:49 PM

Butang onclick HTML

See all articles