PHP batch replace the relative address with an absolute address to implement code _ PHP Tutorial

Source: Internet
Author: User
PHP batch replaces the relative address with the absolute address implementation code. If you need to replace the relative address with an absolute address in PHP batch, refer. The code is as follows: Copy the code? The keyword $ GLOBALS [patterns] array (# Replace the relative address with an absolute address in php in batches. if you need code, refer to it.
The code is as follows:

// The keyword to be replaced
$ GLOBALS ["patterns"] = array (
"/# BASE_URL #/"
);

// The Replaced content corresponds to the above matching rules one by one
$ GLOBALS ["replacements"] = array (
Http://api.map.baidu.com/lbsapi/cloud"
// "Http: // 172.220.178/lbsapi /"
// "Http://dev.baidu.com/wiki/static/map/cloud"
);

/**
* File operations
*
* Example:
* FileUtil: copyDir ('B', 'd/e'); create a d/e folder in the copy folder and copy the content in the B folder.
* FileUtil: copyFile ('B/1/2/3.exe', 'B/B/3.exe'); create a B/B folder for the copied file, copy the 3.exe file in the B/1/2 folder.
* FileUtil: createDir ('a/1/2/3'); create a folder named a/1/2/3
* FileUtil: unlinkFile ('B/d/3.exe'); test deleting a file B/d/3.exe
*/
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 an object
*
* @ Param string $ aimUrl
* @ Return boolean
*/
Function unlinkFile ($ aimUrl ){
If (file_exists ($ aimUrl )){
Unlink ($ aimUrl );
Return true;
} Else {
Return false;
}
}

/**
* Copy a folder
*
* @ Param string $ oldDir
* @ Param string $ aimDir
* @ Param boolean $ overWrite this parameter controls whether to overWrite the original file
* @ 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 an object
*
* @ Param string $ fileUrl
* @ Param string $ aimUrl
* @ Param boolean $ overWrite this parameter controls whether to overWrite the original file
* @ 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 );
// Replace the variable
$ ApiFilePointer = fopen ($ aimUrl, 'r ');
$ ApiFileContent = fread ($ apiFilePointer, filesize ($ aimUrl ));
// Only replace JavaScript, html, and css files
If (preg_match ('/(. js |. html |. css |. htm) $/', $ aimUrl )){
$ ApiFileContent = preg_replace ($ GLOBALS ["patterns"], $ GLOBALS ["replacements"], $ apiFileContent );
}
Fclose ($ apiFilePointer );
Echo $ aimUrl. "rn ";
$ ApiFilePointer = fopen ($ aimUrl, 'W + ');
Fwrite ($ apiFilePointer, $ apiFileContent );
Fclose ($ apiFilePointer );
// Replace the variable
Return true;
}
}

FileUtil: copyDir ("resource", "cloud", true );

?>

Http://www.bkjia.com/PHPjc/631603.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631603.htmlTechArticlePHP bulk replace relative address for absolute address to realize the code has the need of friends can refer. The code is as follows? Php // The keyword to be replaced $ GLOBALS [patterns] = array (/#...

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.