您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)怎么使用php腳本給html中引用的js和css路徑打上版本號(hào),小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
在搜索引擎中搜索關(guān)鍵字.htaccess 緩存,你可以搜索到很多關(guān)于設(shè)置網(wǎng)站文件緩存的教程,通過設(shè)置可以將css、js等不太經(jīng)常更新的文件緩存在瀏覽器端,這樣訪客每次訪問你的網(wǎng)站的時(shí)候,瀏覽器就可以從瀏覽器的緩存中獲取css、js等,而不必從你的服務(wù)器讀取,這樣在一定程度上加快了網(wǎng)站的打開速度,又可以節(jié)約一下你的服務(wù)器流量。
比如
<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>
當(dāng)然如果不是前后端 分離得干干凈凈的,就沒必要這么額外的這樣自己在寫個(gè)腳本去打版本。
打版本的好處:
解決外部引用文件實(shí)時(shí)更新問題。比如
pc端上主要體現(xiàn)在 iframe中的外部引用文件不會(huì)實(shí)時(shí)更新。
wap端上部分app也是比如微信。 如果你的網(wǎng)頁是嵌到自己的app,那也更不用說了。
用php寫了個(gè)類
//生成版本 //清除版本 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('必須在內(nèi)網(wǎng)環(huán)境 10.10.0開頭才可運(yùn)行'); //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節(jié)點(diǎn) * @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節(jié)點(diǎn) * @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); } /** * 回調(diào)模式匹配 * @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) {//沒有問號(hào) $arr[0] .= '?'. $versionName. '='. $version; }else{//有問號(hào) 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; } /** * 執(zhí)行 */ 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; } /** * 設(shè)置需要操作的后綴 */ 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; //第一個(gè)參就是版本信息, 第二個(gè)就是要匹配的文件后綴, 第三個(gè)是要匹配的目錄或者文件 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(); }
關(guān)于“怎么使用php腳本給html中引用的js和css路徑打上版本號(hào)”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。