如果你的url链接是相对路径static/mapi.css,你想把他批量替换成绝对路径http://dev.baidu.com/wiki/static/map/cloud/static/mapi.css。那么,你可以这样做: 写一个PHP文件,把需要替换的网址写进去。 这个代码的意思就是,把#BASE_URL#替换成http://api.m
如果你的url链接是相对路径“static/mapi.css”,你想把他批量替换成绝对路径“http://dev.baidu.com/wiki/static/map/cloud/static/mapi.css”。那么,你可以这样做:
写一个PHP文件,把需要替换的网址写进去。
这个代码的意思就是,把#BASE_URL#替换成 http://api.map.baidu.com/lbsapi/cloud/ 。
这句话的意思是,把resource文件里的内容替换一遍,然后放到cloud文件夹里。FileUtil::copyDir("resource","cloud",true);
<span>php </span><span>//</span><span>需要替换的关键字</span> <span>$GLOBALS</span>["patterns"] = <span>array</span><span>( </span>"/#BASE_URL#/"<span> ); </span><span>//</span><span>替换后的内容,和上面的匹配规则一一对应</span> <span>$GLOBALS</span>["replacements"] = <span>array</span><span>( </span>"http://api.map.baidu.com/lbsapi/cloud/" <span>//</span><span>"http://172.22.168.178/lbsapi/" //"http://dev.baidu.com/wiki/static/map/cloud/"</span> <span>); </span><span>/*</span><span>* * 操纵文件类 * * 例子: * FileUtil::copyDir('b','d/e'); 测试复制文件夹 建立一个d/e文件夹,把b文件夹下的内容复制进去 * FileUtil::copyFile('b/1/2/3.exe','b/b/3.exe'); 测试复制文件 建立一个b/b文件夹,并把b/1/2文件夹中的3.exe文件复制进去 * FileUtil::createDir('a/1/2/3'); 测试建立文件夹 建一个a/1/2/3文件夹 * FileUtil::unlinkFile('b/d/3.exe'); 测试删除文件 删除b/d/3.exe文件 </span><span>*/</span> <span>class</span><span> FileUtil { </span><span>/*</span><span>* * 建立文件夹 * * @param string $aimUrl * @return viod </span><span>*/</span> <span>function</span> createDir(<span>$aimUrl</span><span>) { </span><span>$aimUrl</span> = <span>str_replace</span>('', '/', <span>$aimUrl</span><span>); </span><span>$aimDir</span> = ''<span>; </span><span>$arr</span> = <span>explode</span>('/', <span>$aimUrl</span><span>); </span><span>foreach</span> (<span>$arr</span> <span>as</span> <span>$str</span><span>) { </span><span>$aimDir</span> .= <span>$str</span> . '/'<span>; </span><span>if</span> (!<span>file_exists</span>(<span>$aimDir</span><span>)) { </span><span>mkdir</span>(<span>$aimDir</span><span>); } } } </span><span>/*</span><span>* * 删除文件 * * @param string $aimUrl * @return boolean </span><span>*/</span> <span>function</span> unlinkFile(<span>$aimUrl</span><span>) { </span><span>if</span> (<span>file_exists</span>(<span>$aimUrl</span><span>)) { </span><span>unlink</span>(<span>$aimUrl</span><span>); </span><span>return</span> <span>true</span><span>; } </span><span>else</span><span> { </span><span>return</span> <span>false</span><span>; } } </span><span>/*</span><span>* * 复制文件夹 * * @param string $oldDir * @param string $aimDir * @param boolean $overWrite 该参数控制是否覆盖原文件 * @return boolean </span><span>*/</span> <span>function</span> copyDir(<span>$oldDir</span>, <span>$aimDir</span>, <span>$overWrite</span> = <span>false</span><span>) { </span><span>$aimDir</span> = <span>str_replace</span>('', '/', <span>$aimDir</span><span>); </span><span>$aimDir</span> = <span>substr</span>(<span>$aimDir</span>, -1) == '/' ? <span>$aimDir</span> : <span>$aimDir</span>.'/'<span>; </span><span>$oldDir</span> = <span>str_replace</span>('', '/', <span>$oldDir</span><span>); </span><span>$oldDir</span> = <span>substr</span>(<span>$oldDir</span>, -1) == '/' ? <span>$oldDir</span> : <span>$oldDir</span>.'/'<span>; </span><span>if</span> (!<span>is_dir</span>(<span>$oldDir</span><span>)) { </span><span>return</span> <span>false</span><span>; } </span><span>if</span> (!<span>file_exists</span>(<span>$aimDir</span><span>)) { FileUtil</span>::createDir(<span>$aimDir</span><span>); } </span><span>$dirHandle</span> = <span>opendir</span>(<span>$oldDir</span><span>); </span><span>while</span>(<span>false</span> !== (<span>$file</span> = <span>readdir</span>(<span>$dirHandle</span><span>))) { </span><span>if</span> (<span>$file</span> == '.' || <span>$file</span> == '..'<span>) { </span><span>continue</span><span>; } </span><span>if</span> (!<span>is_dir</span>(<span>$oldDir</span> . <span>$file</span><span>)) { FileUtil</span>::copyFile(<span>$oldDir</span> . <span>$file</span>, <span>$aimDir</span> . <span>$file</span>, <span>$overWrite</span><span>); } </span><span>else</span><span> { FileUtil</span>::copyDir(<span>$oldDir</span> . <span>$file</span>, <span>$aimDir</span> . <span>$file</span>, <span>$overWrite</span><span>); } } </span><span>return</span> <span>closedir</span>(<span>$dirHandle</span><span>); } </span><span>/*</span><span>* * 复制文件 * * @param string $fileUrl * @param string $aimUrl * @param boolean $overWrite 该参数控制是否覆盖原文件 * @return boolean </span><span>*/</span> <span>function</span> copyFile(<span>$fileUrl</span>, <span>$aimUrl</span>, <span>$overWrite</span> = <span>false</span><span>) { </span><span>if</span> (!<span>file_exists</span>(<span>$fileUrl</span><span>)) { </span><span>return</span> <span>false</span><span>; } </span><span>if</span> (<span>file_exists</span>(<span>$aimUrl</span>) && <span>$overWrite</span> == <span>false</span><span>) { </span><span>return</span> <span>false</span><span>; } </span><span>elseif</span> (<span>file_exists</span>(<span>$aimUrl</span>) && <span>$overWrite</span> == <span>true</span><span>) { FileUtil</span>::unlinkFile(<span>$aimUrl</span><span>); } </span><span>$aimDir</span> = <span>dirname</span>(<span>$aimUrl</span><span>); FileUtil</span>::createDir(<span>$aimDir</span><span>); </span><span>copy</span>(<span>$fileUrl</span>, <span>$aimUrl</span><span>); </span><span>//</span><span>替换变量</span> <span>$apiFilePointer</span> = <span>fopen</span>(<span>$aimUrl</span>, 'r'<span>); </span><span>$apiFileContent</span> = <span>fread</span>(<span>$apiFilePointer</span>, <span>filesize</span>(<span>$aimUrl</span><span>)); </span><span>//</span><span>只在js、html、css文件进行替换</span> <span>if</span> (<span>preg_match</span>('/(\.js|\.html|\.css|\.htm)$/', <span>$aimUrl</span><span>)) { </span><span>$apiFileContent</span> = <span>preg_replace</span>(<span>$GLOBALS</span>["patterns"], <span>$GLOBALS</span>["replacements"], <span>$apiFileContent</span><span>); } </span><span>fclose</span>(<span>$apiFilePointer</span><span>); </span><span>echo</span> <span>$aimUrl</span>."\r\n"<span>; </span><span>$apiFilePointer</span> = <span>fopen</span>(<span>$aimUrl</span>, 'w+'<span>); </span><span>fwrite</span>(<span>$apiFilePointer</span>, <span>$apiFileContent</span><span>); </span><span>fclose</span>(<span>$apiFilePointer</span><span>); </span><span>//</span><span>替换变量</span> <span>return</span> <span>true</span><span>; } } FileUtil</span>::copyDir("resource","cloud",<span>true</span><span>); </span>?>
再写一个bat批处理文件,来运行这个PHP。
php release.php
现在,只需点击bat文件,全部页面里的相对地址就会变成绝对地址了。