前言
大家應該都有所體會,很多時候在做網絡爬蟲的時候特別需要將爬蟲搜索到的超鏈接進行處理,統一都改成絕對路徑的,所以本文就寫了一個正則表達式來對搜索到的鏈接進行處理。下面話不多說,來看看詳細的介紹吧。
通常我們可能會搜索到如下的鏈接:
<!-- 空超鏈接 --><a href=""></a> <!-- 空白符 --><a href=" " rel="external nofollow" > </a><!-- a標簽含有其它屬性 --><a href="index.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" $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)總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對VeVb武林網的支持。
新聞熱點
疑難解答
圖片精選