PHP利用正則表達(dá)式將相對(duì)路徑轉(zhuǎn)成絕對(duì)路徑的方法示例
前言
大家應(yīng)該都有所體會(huì),很多時(shí)候在做網(wǎng)絡(luò)爬蟲(chóng)的時(shí)候特別需要將爬蟲(chóng)搜索到的超鏈接進(jìn)行處理,統(tǒng)一都改成絕對(duì)路徑的,所以本文就寫(xiě)了一個(gè)正則表達(dá)式來(lái)對(duì)搜索到的鏈接進(jìn)行處理。下面話不多說(shuō),來(lái)看看詳細(xì)的介紹吧。
通常我們可能會(huì)搜索到如下的鏈接:
<!-- 空超鏈接 --> <a href=""></a> <!-- 空白符 --> <a href=" " rel="external nofollow" > </a> <!-- a標(biāo)簽含有其它屬性 --> <a href="index.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" alt="超鏈接"> index.html </a> <a href="/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" target="_blank"> / target="_blank" </a> <a target="_blank" href="/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" alt="超鏈接" > target="_blank" / alt="超鏈接" </a> <a target="_blank" title="超鏈接" href="/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" alt="超鏈接" > target="_blank" title="超鏈接" / alt="超鏈接" </a> <!-- 根目錄 --> <a href="/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > / </a> <a href="a" rel="external nofollow" > a </a> <!-- 含參數(shù) --> <a href="/index.html?id=1" rel="external nofollow" > /index.html?id=1 </a> <a href="?id=2" rel="external nofollow" > ?id=2 </a> <!-- // --> <a rel="external nofollow" > //index.html </a> <a rel="external nofollow" > //www.mafutian.net </a> <!-- 站內(nèi)鏈接 --> <a rel="external nofollow" > http://www.hole_1.com/index.html </a> <!-- 站外鏈接 --> <a rel="external nofollow" > http://www.mafutian.net </a> <a rel="external nofollow" > http://www.numberer.net </a> <!-- 圖片,文本文件格式的鏈接 --> <a href="1.jpg" rel="external nofollow" > 1.jpg </a> <a href="1.jpeg" rel="external nofollow" > 1.jpeg </a> <a href="1.gif" rel="external nofollow" > 1.gif </a> <a href="1.png" rel="external nofollow" > 1.png </a> <a href="1.txt" rel="external nofollow" > 1.txt </a> <!-- 普通鏈接 --> <a href="index.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" > index.html </a> <a href="index.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" > index.html </a> <a href="./index.html" rel="external nofollow" > ./index.html </a> <a href="../index.html" rel="external nofollow" > ../index.html </a> <a href=".../" rel="external nofollow" > .../ </a> <a href="..." rel="external nofollow" > ... </a> <!-- 非鏈接,含有鏈接冒號(hào) --> <a href="javascript:void(0)" rel="external nofollow" > javascript:void(0) </a> <a href="a:b" rel="external nofollow" > a:b </a> <a href="/a#a:b" rel="external nofollow" > /a#a:b </a> <a href="mailto:'mafutian@126.com'" rel="external nofollow" > mailto:'mafutian@126.com' </a> <a href="/tencent://message/?uin=335134463" rel="external nofollow" > /tencent://message/?uin=335134463 </a> <!-- 相對(duì)路徑 --> <a href="." rel="external nofollow" > . </a> <a href=".." rel="external nofollow" > .. </a> <a href="../" rel="external nofollow" > ../ </a> <a href="/a/b/.." rel="external nofollow" > /a/b/.. </a> <a href="/a" rel="external nofollow" > /a </a> <a href="./b" rel="external nofollow" > ./b </a> <a href="./././././././././b" rel="external nofollow" > ./././././././././b </a> <!-- 其實(shí)就是 ./b --> <a href="../c" rel="external nofollow" > ../c </a> <a href="../../d" rel="external nofollow" > ../../d </a> <a href="../a/../b/c/../d" rel="external nofollow" > ../a/../b/c/../d </a> <a href="./../e" rel="external nofollow" > ./../e </a> <a rel="external nofollow" > http://www.hole_1.org/./../e </a> <a href="./.././f" rel="external nofollow" > ./.././f </a> <a rel="external nofollow" > http://www.hole_1.org/../a/.../../b/c/../d/.. </a> <!-- 帶有端口號(hào) --> <a href=":8081/index.html" rel="external nofollow" > :8081/index.html </a> <a rel="external nofollow" > :80/index.html </a> <a rel="external nofollow" > http://www.mafutian.net:8081/index.html </a> <a rel="external nofollow" > http://www.mafutian.net:8082/index.html </a>
處理的第一步,設(shè)置成絕對(duì)路徑:
http:// ... / ../ ../
然后本文講講如何去除絕對(duì)路徑中的 './'、'../'、'/..'的實(shí)現(xiàn)代碼:
function url_to_absolute($relative)
{
$absolute = '';
// 去除所有的 './'
$absolute = preg_replace('/(?<!\.)\.\//','',$relative);
$count = preg_match_all('/(?<!\/)\/([^\/]{1,}?)\/\.\.\//',$absolute,$res);
// 迭代去除所有的 '/abc/../'
do
{
$absolute = preg_replace('/(?<!\/)\/([^\/]{1,}?)\/\.\.\//','/',$absolute);
$count = preg_match_all('/(?<!\/)\/([^\/]{1,}?)\/\.\.\//',$absolute,$res);
}while($count >= 1);
// 除去最后的 '/..'
$absolute = preg_replace('/(?<!\/)\/([^\/]{1,}?)\/\.\.$/','/',$absolute);
$absolute = preg_replace('/\/\.\.$/','',$absolute);
// 除去存在的 '../'
$absolute = preg_replace('/(?<!\.)\.\.\//','',$absolute);
return $absolute;
}
$relative = 'http://www.mytest.org/../a/.../../b/c/../d/..';
var_dump(url_to_absolute($relative));
// 輸出:string 'http://www.mytest.org/a/b/' (length=26)
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- 談?wù)凱HP中相對(duì)路徑的問(wèn)題與絕對(duì)路徑的使用
- PHP文件操作之獲取目錄下文件與計(jì)算相對(duì)路徑的方法
- php命令行(cli)下執(zhí)行PHP腳本文件的相對(duì)路徑的問(wèn)題解決方法
- php計(jì)算兩個(gè)文件相對(duì)路徑的方法
- PHP獲取文件相對(duì)路徑的方法
- php求兩個(gè)目錄的相對(duì)路徑示例(php獲取相對(duì)路徑)
- 一道求$b相對(duì)于$a的相對(duì)路徑的php代碼
- php絕對(duì)路徑與相對(duì)路徑之間關(guān)系的的分析
- php zend 相對(duì)路徑問(wèn)題
- php 算法之實(shí)現(xiàn)相對(duì)路徑的實(shí)例
相關(guān)文章
PHP中return 和 exit 、break和contiue 區(qū)別與用法
return、break和contiue是語(yǔ)言結(jié)構(gòu),就如同if語(yǔ)句之類(lèi)的,但是exit卻是個(gè)函數(shù)2012-04-04
php外部執(zhí)行命令函數(shù)用法小結(jié)
這篇文章主要介紹了php外部執(zhí)行命令函數(shù)用法,結(jié)合實(shí)例形式分析了exec與system執(zhí)行外部命令的相關(guān)使用技巧,需要的朋友可以參考下2016-10-10
PHP入門(mén)教程之?dāng)?shù)組用法匯總(創(chuàng)建,刪除,遍歷,排序等)
這篇文章主要介紹了PHP入門(mén)教程之?dāng)?shù)組用法,結(jié)合大量實(shí)例總結(jié)分析了php關(guān)于數(shù)組的創(chuàng)建、打印、遍歷、獲取、排序、插入、刪除等常見(jiàn)操作技巧,需要的朋友可以參考下2016-09-09
php使用文本統(tǒng)計(jì)訪問(wèn)量的方法
這篇文章主要介紹了php使用文本統(tǒng)計(jì)訪問(wèn)量的方法,涉及php文本文件讀寫(xiě)與數(shù)值運(yùn)算的相關(guān)技巧,需要的朋友可以參考下2016-05-05
php使用sql數(shù)據(jù)庫(kù) 獲取字段問(wèn)題介紹
由于sql server中,ntext和nvarchar字段是用unicode編碼存儲(chǔ)內(nèi)容的,因此php通過(guò)mssql擴(kuò)展讀取帶ntext和nvarchar類(lèi)型字段的時(shí)候會(huì)抱錯(cuò)2013-08-08

