无为清净楼资源网 Design By www.qnjia.com

前言

大家应该都有所体会,很多时候在做网络爬虫的时候特别需要将爬虫搜索到的超链接进行处理,统一都改成绝对路径的,所以本文就写了一个正则表达式来对搜索到的链接进行处理。下面话不多说,来看看详细的介绍吧。

通常我们可能会搜索到如下的链接:

<!-- 空超链接 -->
<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" rel="external nofollow" > /index.html"" rel="external nofollow" > "//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.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/" rel="external nofollow" > /tencent://message/"." 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>

处理的第一步,设置成绝对路径:

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

然后本文讲讲如何去除绝对路径中的 './'、'../'、'/..'的实现代码:

function url_to_absolute($relative)
{
 $absolute = '';
 // 去除所有的 './'
 $absolute = preg_replace('/("color: #ff0000">总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。

标签:
php相对路径,绝对路径,正则表达式,相对路径,相对路径转绝对路径

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?