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 相关文章推荐
php join函数应用
May 04 PHP
PHP设计模式 注册表模式
Feb 05 PHP
web server使用php生成web页面的三种方法总结
Oct 28 PHP
PHP echo,print,printf,sprintf函数之间的区别与用法详解
Nov 27 PHP
php中spl_autoload详解
Oct 17 PHP
php跨站攻击实例分析
Oct 28 PHP
PHP实现微信模拟登陆并给用户发送消息的方法【文字,图片,图文】
Jun 29 PHP
win10 apache配置虚拟主机后localhost无法使用的解决方法
Jan 27 PHP
php+mysql开发中的经验与常识小结
Mar 25 PHP
Yii框架中用response保存cookie,用request读取cookie的原理解析
Sep 04 PHP
Laravel 验证码认证学习记录小结
Dec 20 PHP
php文件上传原理与实现方法详解
Dec 20 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自动更新新闻DIY
2006/10/09 PHP
PHP数据集构建JSON格式及新数组的方法
2012/11/07 PHP
深入解析fsockopen与pfsockopen的区别
2013/07/05 PHP
PHP递归遍历多维数组实现无限分类的方法
2016/05/06 PHP
PHP的JSON封装、转变及输出操作示例
2019/09/27 PHP
jquery如何把参数列严格转换成数组实现思路
2013/04/01 Javascript
js仿土豆网带缩略图的焦点图片切换效果实现方法
2015/02/23 Javascript
去除字符串左右两边的空格(实现代码)
2016/05/12 Javascript
Webpack如何引入bootstrap的方法
2017/06/17 Javascript
JS实现闭包中的沙箱模式示例
2017/09/07 Javascript
微信小程序上传图片实例
2018/05/28 Javascript
利用vue-i18n实现多语言切换效果的方法
2019/06/19 Javascript
vue获取验证码倒计时组件
2019/08/26 Javascript
微信小程序添加插屏广告并设置显示频率(一天一次)
2019/12/06 Javascript
原生js实现五子棋游戏
2020/05/28 Javascript
利用 Chrome Dev Tools 进行页面性能分析的步骤说明(前端性能优化)
2021/02/24 Javascript
[31:00]2014 DOTA2华西杯精英邀请赛5 24 NewBee VS iG
2014/05/25 DOTA
[54:27]TNC vs Serenity 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
[01:31:02]TNC vs VG 2019国际邀请赛淘汰赛 胜者组赛BO3 第一场
2019/08/22 DOTA
[03:17]DOTA2-DPC中国联赛1月29日Recap集锦
2021/03/11 DOTA
Matplotlib中文乱码的3种解决方案
2018/11/15 Python
python调用Matplotlib绘制分布点图
2019/10/18 Python
Python+PyQt5+MySQL实现天气管理系统
2020/06/16 Python
python的链表基础知识点
2020/09/13 Python
python用opencv 图像傅里叶变换
2021/01/04 Python
英国婴儿和儿童服装网站:Vertbaudet
2018/04/02 全球购物
英国顶级珠宝品牌之家:John Greed
2018/06/09 全球购物
Currentbody澳大利亚:美容仪专家
2019/11/11 全球购物
数据库的约束含义
2012/09/09 面试题
农业大学毕业生的个人自我评价
2013/10/11 职场文书
医学实习生自我鉴定
2013/12/12 职场文书
《听鱼说话》教学反思
2014/02/15 职场文书
乳制品整治工作方案
2014/05/29 职场文书
销售开票员岗位职责
2015/04/15 职场文书
停车场管理制度范本
2015/08/05 职场文书
2016年学校“6﹒26国际禁毒日”宣传活动总结
2016/04/05 职场文书