How to use PHP script to _php the version number of JS and CSS paths referenced in HTML

Source: Internet
Author: User
Tags explode mixed php script

Search for keywords in search engines. htaccess cache, you can search for a lot of information about setting up Web site file caching tutorial, through the settings can be CSS, JS and other infrequently updated files cached in the browser side, so that visitors each visit your site, Browser can be from the browser's cache to get CSS, JS, and so on, and do not have to read from your server, so to some extent, speed up the opening of the site, but also to save your server traffic.

The concrete text explanation does not give everybody to say more, below through the code example gives everybody to explain.

Like what

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

The href and SRC versions are added to the

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

Of course, if the front and back ends are clean, there is no need to write a script to make a version of it so extra.

The benefits of playing a version:

Solve the problem of external reference file real time update. Like what

External reference files that are mainly embodied in the IFRAME on the PC side are not updated in real time.

Some of the apps on the WAP side are also like micro-letters. If your Web page is embedded in your app, let alone.

Write a class in PHP

Build Version//Clear version 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 (' must start in the intranet environment 10.10.0 to run ' );
  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; /** * matches the script * @return string that is matched to the script node * @param array $match to handle the script/protected function callbacks Cript ($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); /** * Matches CSS * @param array $match matching CSS * @return string handles 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); /** * Callback Pattern Matching * @param string $str * @param string $pattern * @return string/protected function Callbac  Kmatch ($str, $pattern) {switch ($this->dealflag) {case ' replace ': Return $this->replacecallbackmatch ($STR,
   $pattern);
   Case "clean": Return $this->cleancallbackmatch ($STR, $pattern);
  Default: $this->error (' illegal mode '); }/** * Replace version * @param string $str to be processed string * @param string $pattern Regular * @return string with string/PR otected 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) {//No question mark $arr [0]. = '? '. $versionName. '='.
  $version; }else{//has a question mark if (Preg_match (^|\&) '. $versionName. ' =(.*?) ($|\&)/', $arr [1])) {//exists $arr [1] = Preg_replace ('/(^|\&) '. $versionName. ' =(.*?) ($|\&)/', ' $ '. $versionName. ' ='. $version.
    ' $ ', $arr [1]); $arr [0]. = '? '.
   $arr [1]; }else{//does not exist $arr [0]. = '? '. $arr [1]. ' & '. $versionName. '='.
   $version; return $third [1]. $arr [0].
 $third [3];  /** * Clears version * @param string $str The version to be purged * @param string $pattern Regular * @return string cleared 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. ') =(.*?)
   ($|\&)/', ' $ ', $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;
   /** * Execute */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 ');
  }/** * Add version */public function replace () {$this->dealflag = ' replace ';
  $this->run ();
 Echo ' replace success ';
  /** * Purge Version/Public function clear () {$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); /** * Obtained version * @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 '); }/** * Determines whether the file will add or clear the version * @param string $profix suffix * @return boolean true by suffix |  False */protected function Isneedreplacepostfix ($profix) {if (In_array ($profix, $this->filepostfixs)) {return
  True
 return false; /** * Set the suffix that requires action */Public Function Setfilepostfix ($profix) {if (Is_array) {count ($profix) > 0 &A mp;& ($this->filepostfixs = Array_merge ($this->filepoStfixs, $profix));
  }else if (is_string ($profix)) {$this->filepostfixs[] = $profix; }
 }
}

Use:

$dir  = __dir__;
$is _clean = false;
$is _clean = true;
The first parameter is the version information, the second is the file suffix to match, the third is the directory to match or file
if ($is _clean) {//clear version
 $configs = ' Eslc-wap ';
 $REPLACEOBJ = new Replaceversion ($configs, Array (' HTML '), $dir);
 $REPLACEOBJ->clean ();
} else{//Add or replace version
 $configs = Array (' Eslc-wap ' => ' 1.0.1 ');//can also be written as $configs = ' eslc-wap, 1.0.1 ';
 $REPLACEOBJ = new Replaceversion ($configs, Array (' HTML '), $dir);
 $REPLACEOBJ->replace ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.