How to batch substitute relative address for absolute address of using bat batch implementation

  • 2020-06-01 09:18:34
  • OfStack

If your url link is a relative path "static/mapi. css", do you want to batch replace him into an absolute path "http: / / dev. baidu. com/wiki/static/map cloud/static/mapi css". So, here's what you can do:

Write an PHP file and include the url that needs to be replaced.
The meaning of this code is that the # # BASE_URL replace http: / / api map. baidu. com/lbsapi/cloud /.
This means replacing the contents of the resource file once and placing it in the cloud folder. FileUtil: : copyDir (" resource ", "cloud", true);
 
<?php 
// Keywords that need to be replaced  
$GLOBALS["patterns"] = array( 
"/#BASE_URL#/" 
); 
// Replace the content with the matching rule above 11 The corresponding  
$GLOBALS["replacements"] = array( 
"http://api.map.baidu.com/lbsapi/cloud/" 
//"http://172.22.168.178/lbsapi/" 
//"http://dev.baidu.com/wiki/static/map/cloud/" 
); 
/** 
*  Manipulate file class  
* 
*  Example:  
* FileUtil::copyDir('b','d/e');  Test copy folder   To establish 1 a d/e Folder, put b Copy the contents of the folder  
* FileUtil::copyFile('b/1/2/3.exe','b/b/3.exe');  Test copy file   To establish 1 a b/b Folder and put b/1/2 In a folder 3.exe Copy the file in  
* FileUtil::createDir('a/1/2/3');  Test create folder   build 1 a a/1/2/3 folder  
* FileUtil::unlinkFile('b/d/3.exe');  Test delete file   delete b/d/3.exe file  
*/ 
class FileUtil { 
/** 
*  Create a folder  
* 
* @param string $aimUrl 
* @return viod 
*/ 
function createDir($aimUrl) { 
$aimUrl = str_replace('', '/', $aimUrl); 
$aimDir = ''; 
$arr = explode('/', $aimUrl); 
foreach ($arr as $str) { 
$aimDir .= $str . '/'; 
if (!file_exists($aimDir)) { 
mkdir($aimDir); 
} 
} 
} 
/** 
*  Delete the file  
* 
* @param string $aimUrl 
* @return boolean 
*/ 
function unlinkFile($aimUrl) { 
if (file_exists($aimUrl)) { 
unlink($aimUrl); 
return true; 
} else { 
return false; 
} 
} 
/** 
*  Copy folder  
* 
* @param string $oldDir 
* @param string $aimDir 
* @param boolean $overWrite  This parameter controls whether the original file is overwritten  
* @return boolean 
*/ 
function copyDir($oldDir, $aimDir, $overWrite = false) { 
$aimDir = str_replace('', '/', $aimDir); 
$aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir.'/'; 
$oldDir = str_replace('', '/', $oldDir); 
$oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir.'/'; 
if (!is_dir($oldDir)) { 
return false; 
} 
if (!file_exists($aimDir)) { 
FileUtil::createDir($aimDir); 
} 
$dirHandle = opendir($oldDir); 
while(false !== ($file = readdir($dirHandle))) { 
if ($file == '.' || $file == '..') { 
continue; 
} 
if (!is_dir($oldDir . $file)) { 
FileUtil::copyFile($oldDir . $file, $aimDir . $file, $overWrite); 
} else { 
FileUtil::copyDir($oldDir . $file, $aimDir . $file, $overWrite); 
} 
} 
return closedir($dirHandle); 
} 
/** 
*  Copy the file  
* 
* @param string $fileUrl 
* @param string $aimUrl 
* @param boolean $overWrite  This parameter controls whether the original file is overwritten  
* @return boolean 
*/ 
function copyFile($fileUrl, $aimUrl, $overWrite = false) { 
if (!file_exists($fileUrl)) { 
return false; 
} 
if (file_exists($aimUrl) && $overWrite == false) { 
return false; 
} elseif (file_exists($aimUrl) && $overWrite == true) { 
FileUtil::unlinkFile($aimUrl); 
} 
$aimDir = dirname($aimUrl); 
FileUtil::createDir($aimDir); 
copy($fileUrl, $aimUrl); 
// Substitution variables  
$apiFilePointer = fopen($aimUrl, 'r'); 
$apiFileContent = fread($apiFilePointer, filesize($aimUrl)); 
// Only in the js , html , css File replacement  
if (preg_match('/(\.js|\.html|\.css|\.htm)$/', $aimUrl)) { 
$apiFileContent = preg_replace($GLOBALS["patterns"], $GLOBALS["replacements"], $apiFileContent); 
} 
fclose($apiFilePointer); 
echo $aimUrl."\r\n"; 
$apiFilePointer = fopen($aimUrl, 'w+'); 
fwrite($apiFilePointer, $apiFileContent); 
fclose($apiFilePointer); 
// Substitution variables  
return true; 
} 
} 
FileUtil::copyDir("resource","cloud",true); 
?> 

Write another bat batch file to run the PHP.
php release.php
Now, just click on the bat file and the relative address in the entire page will become the absolute address.

Related articles: