今天就跟大家聊聊有關(guān)怎么在PHP中使用正則表達(dá)式將相對路徑轉(zhuǎn)換成絕對路徑,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

通常我們可能會搜索到如下的鏈接:
<!-- 空超鏈接 --> <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 href="//index.html" rel="external nofollow" > //index.html </a> <a href="//www.mafutian.net" rel="external nofollow" > //www.mafutian.net </a> <!-- 站內(nèi)鏈接 --> <a href="http://www.hole_1.com/index.html" rel="external nofollow" > http://www.hole_1.com/index.html </a> <!-- 站外鏈接 --> <a href="http://www.mafutian.net" rel="external nofollow" > http://www.mafutian.net </a> <a href="http://www.numberer.net" 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> <!-- 非鏈接,含有鏈接冒號 --> <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> <!-- 相對路徑 --> <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> <!-- 其實就是 ./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 href="http://www.hole_1.org/./../e" rel="external nofollow" > http://www.hole_1.org/./../e </a> <a href="./.././f" rel="external nofollow" > ./.././f </a> <a href="http://www.hole_1.org/../a/.../../b/c/../d/.." rel="external nofollow" > http://www.hole_1.org/../a/.../../b/c/../d/.. </a> <!-- 帶有端口號 --> <a href=":8081/index.html" rel="external nofollow" > :8081/index.html </a> <a href="http://www.mafutian.net:80/index.html" rel="external nofollow" > :80/index.html </a> <a href="http://www.mafutian.net:8081/index.html" rel="external nofollow" > http://www.mafutian.net:8081/index.html </a> <a href="http://www.mafutian.net:8082/index.html" rel="external nofollow" > http://www.mafutian.net:8082/index.html </a>
處理的第一步,設(shè)置成絕對路徑:
http:// ... / ../ ../
然后本文講講如何去除絕對路徑中的 './'、'../'、'/..'的實現(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 '/tupian/20230522/&>看完上述內(nèi)容,你們對怎么在PHP中使用正則表達(dá)式將相對路徑轉(zhuǎn)換成絕對路徑有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
本文名稱:怎么在PHP中使用正則表達(dá)式將相對路徑轉(zhuǎn)換成絕對路徑-創(chuàng)新互聯(lián)
分享路徑:http://chinadenli.net/article14/cddide.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)站設(shè)計公司、自適應(yīng)網(wǎng)站、做網(wǎng)站、移動網(wǎng)站建設(shè)、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源:
創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容