PHP利用正则表达式将相对路径转成绝对路径的方法示例


Posted in PHP onFebruary 28, 2017

前言

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

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

<!-- 空超链接 -->
<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.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>

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

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

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

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 'http://www.mytest.org/a/b/' (length=26)

总结

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

PHP 相关文章推荐
Access数据库导入Mysql的方法之一
Oct 09 PHP
最令PHP初学者们头痛的十四个问题
Jan 15 PHP
PHP代码保护--Zend Guard的使用详解
Jun 03 PHP
深入分析使用mysql_fetch_object()以对象的形式返回查询结果
Jun 05 PHP
解析关于java,php以及html的所有文件编码与乱码的处理方法汇总
Jun 24 PHP
Windows7下的php环境配置教程
Feb 28 PHP
什么是PEAR?什么是PECL?PHP中两个容易混淆的概念解释
Jul 01 PHP
PHP+jQuery翻板抽奖功能实现
Oct 19 PHP
PHP面向对象继承用法详解(优化与减少代码重复)
Dec 02 PHP
laravel如何开启跨域功能示例详解
Aug 31 PHP
PHP PDOStatement::fetchColumn讲解
Jan 31 PHP
深入理解PHP+Mysql分布式事务与解决方案
Dec 03 PHP
PHP用正则匹配form表单中所有元素的类型和属性值实例代码
Feb 28 #PHP
PHP中让json_encode不自动转义斜杠“/”的方法
Feb 28 #PHP
PHP连接MYSQL数据库的3种常用方法
Feb 27 #PHP
php获取今日开始时间和结束时间的方法
Feb 27 #PHP
php+mysql+jquery实现日历签到功能
Feb 27 #PHP
php查找字符串中第一个非0的位置截取
Feb 27 #PHP
php实时倒计时功能实现方法详解
Feb 27 #PHP
You might like
PHP比你想象的好得多
2014/11/27 PHP
1亿条数据如何分表100张到Mysql数据库中(PHP)
2015/07/29 PHP
PHP  实现等比压缩图片尺寸和大小实例代码
2016/10/08 PHP
PHP编程 SSO详细介绍及简单实例
2017/01/13 PHP
PHP 实现页面静态化的几种方法
2017/07/23 PHP
JS 自动完成 AutoComplete(Ajax 查询)
2009/07/07 Javascript
JQuery从头学起第三讲
2010/07/06 Javascript
用Javascript实现Windows任务管理器的代码
2012/03/27 Javascript
js实现获取当前时间是本月第几周的方法
2015/08/11 Javascript
Javascript中 toFixed四舍六入方法
2017/08/21 Javascript
浅谈AngularJs 双向绑定原理(数据绑定机制)
2017/12/07 Javascript
vue 使用自定义指令实现表单校验的方法
2018/08/28 Javascript
使vue实现jQuery调用的两种方法
2019/05/12 jQuery
vue使用高德地图根据坐标定位点的实现代码
2019/08/22 Javascript
js 实现watch监听数据变化的代码
2019/10/13 Javascript
uni-app从安装到卸载的入门教程
2020/05/15 Javascript
[42:11]TNC vs Pain 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/20 DOTA
python调用新浪微博API项目实践
2014/07/28 Python
Python中强大的命令行库click入门教程
2016/12/26 Python
python中使用正则表达式的后向搜索肯定模式(推荐)
2017/11/11 Python
Python使用pandas处理CSV文件的实例讲解
2018/06/22 Python
python爬取指定微信公众号文章
2018/12/20 Python
Python-Tkinter Text输入内容在界面显示的实例
2019/07/12 Python
Selenium启动Chrome时配置选项详解
2020/03/18 Python
免税水晶:Duty Free Crystal
2019/05/13 全球购物
测绘工程专业个人自我评价
2013/12/01 职场文书
评析教师个人的自我评价
2014/02/19 职场文书
商铺租赁意向书
2014/04/01 职场文书
微笑服务演讲稿
2014/05/13 职场文书
挂牌仪式策划方案
2014/05/18 职场文书
委托书范本
2014/09/13 职场文书
预备党员群众路线教育实践活动思想汇报2014
2014/10/25 职场文书
学籍证明模板
2014/11/21 职场文书
大学生就业指导课心得体会
2016/01/15 职场文书
Android Studio 计算器开发
2022/05/20 Java/Android
MySQL表字段数量限制及行大小限制详情
2022/07/23 MySQL