首頁  >  文章  >  後端開發  >  PHP利用正規表示式將相對路徑轉成絕對路徑的方法

PHP利用正規表示式將相對路徑轉成絕對路徑的方法

高洛峰
高洛峰原創
2017-03-01 16:24:551283瀏覽

前言

大家應該都有所體會,很多時候在做網路爬蟲的時候特別需要將爬蟲搜尋到的超連結進行處理,統一都改成絕對路徑的,所以本文就寫了一個正規表示式來處理搜尋到的連結。下面話不多說,來看看詳細的介紹吧。

通常我們可能會搜尋到如下的連結:

#
<!-- 空超链接 -->
<a href=""></a> 
<!-- 空白符 -->
<a href=" " rel="external nofollow" > </a>
<!-- a标签含有其它属性 -->
<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>
<!-- 含参数 -->
<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>
<!-- 站内链接 -->
<a href="http://www.hole_1.com/index.html" rel="external nofollow" > http://www.php.cn/ </a>
<!-- 站外链接 -->
<a href="http://www.mafutian.net" rel="external nofollow" > http://www.php.cn/ </a>
<a href="http://www.numberer.net" rel="external nofollow" > http://www.php.cn/ </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:&#39;mafutian@126.com&#39;" rel="external nofollow" > mailto:&#39;mafutian@126.com&#39; </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.php.cn/ </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.php.cn/ </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.php.cn/:8081/index.html </a>
<a href="http://www.mafutian.net:8082/index.html" rel="external nofollow" > http://www.php.cn/:8082/index.html </a>

##處理的第一步,設定成絕對路徑:

http:// ... / ../ ../

然後本文講講如何去除絕對路徑中的' ./'、'../'、'/..'的實作程式碼:

#

function url_to_absolute($relative)
{
 $absolute = &#39;&#39;;
 // 去除所有的 &#39;./&#39;
 $absolute = preg_replace(&#39;/(?<!\.)\.\//&#39;,&#39;&#39;,$relative);
 $count = preg_match_all(&#39;/(?<!\/)\/([^\/]{1,}?)\/\.\.\//&#39;,$absolute,$res);
 // 迭代去除所有的 &#39;/abc/../&#39;
 do
 {
 $absolute = preg_replace(&#39;/(?<!\/)\/([^\/]{1,}?)\/\.\.\//&#39;,&#39;/&#39;,$absolute);
 $count = preg_match_all(&#39;/(?<!\/)\/([^\/]{1,}?)\/\.\.\//&#39;,$absolute,$res); 
 }while($count >= 1);
 // 除去最后的 &#39;/..&#39;
 $absolute = preg_replace(&#39;/(?<!\/)\/([^\/]{1,}?)\/\.\.$/&#39;,&#39;/&#39;,$absolute);
 $absolute = preg_replace(&#39;/\/\.\.$/&#39;,&#39;&#39;,$absolute);
 // 除去存在的 &#39;../&#39;
 $absolute = preg_replace(&#39;/(?<!\.)\.\.\//&#39;,&#39;&#39;,$absolute);
 return $absolute;
}
$relative = &#39;http://www.mytest.org/../a/.../../b/c/../d/..&#39;;
var_dump(url_to_absolute($relative));
// 输出:string &#39;http://www.mytest.org/a/b/&#39; (length=26)

更多PHP利用正規表達式將相對路徑轉成絕對路徑的方法相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn