前言
大家应该都有所体会,很多时候在做网络爬虫的时候特别需要将爬虫搜索到的超链接进行处理,统一都改成绝对路径的,所以本文就写了一个正则表达式来对搜索到的链接进行处理。下面话不多说,来看看详细的介绍吧。
通常我们可能会搜索到如下的链接:
index.html / target="_blank" target="_blank" / alt="超链接" target="_blank" title="超链接" / alt="超链接" / a /index.html?id=1 ?id=2 //index.html //www.mafutian.net http://www.hole_1.com/index.html http://www.mafutian.net http://www.numberer.net 1.jpg 1.jpeg 1.gif 1.png 1.txt index.html index.html ./index.html ../index.html .../ ... javascript:void(0) a:b /a#a:b mailto:'mafutian@126.com' /tencent://message/?uin=335134463 . .. ../ /a/b/.. /a ./b ./././././././././b ../c ../../d ../a/../b/c/../d ./../e http://www.hole_1.org/./../e ./.././f http://www.hole_1.org/../a/.../../b/c/../d/.. :8081/index.html :80/index.html http://www.mafutian.net:8081/index.html http://www.mafutian.net:8082/index.html
处理的第一步,设置成绝对路径:
http:// ... / ../ ../
然后本文讲讲如何去除绝对路径中的 './'、'../'、'/..'的实现代码:
function url_to_absolute($relative)
{
$absolute = '';
// 去除所有的 './'
$absolute = preg_replace('/(?= 1);
// 除去最后的 '/..'
$absolute = preg_replace('/(?
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对考高分网的支持。



