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类的使用 实例代码讲解
Dec 28 PHP
PHP数组内存耗用太多问题的解决方法
Apr 05 PHP
PHP iconv 解决utf-8和gb2312编码转换问题
Apr 12 PHP
php制作中间带自己定义图片二维码的方法
Jan 27 PHP
windows8.1下Apache+Php+MySQL配置步骤
Oct 30 PHP
网页的分页下标生成代码(PHP后端方法)
Feb 03 PHP
PHP数学运算函数大汇总(经典值得收藏)
Apr 01 PHP
Yii2搭建后台并实现rbac权限控制完整实例教程
Apr 28 PHP
thinkphp框架实现数据添加和显示功能
Jun 29 PHP
PHP读取word文档的方法分析【基于COM组件】
Aug 01 PHP
laravel5.4生成验证码的实例讲解
Aug 05 PHP
PHP __call()方法实现委托示例
May 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 ? EasyUI DataGrid 资料取的方式介绍
2012/11/07 PHP
php curl抓取网页的介绍和推广及使用CURL抓取淘宝页面集成方法
2015/11/30 PHP
PHP封装的字符串加密解密函数
2015/12/18 PHP
PHP的Json中文处理解决方案
2016/09/29 PHP
PHP vsprintf()函数格式化字符串操作原理解析
2020/07/14 PHP
在Javascript中为String对象添加trim,ltrim,rtrim方法
2006/09/22 Javascript
javascript 遍历验证所有文本框的值
2009/08/27 Javascript
Jquery 弹出层插件实现代码
2009/10/24 Javascript
nodejs实用示例 缩址还原
2010/12/28 NodeJs
jQuery插件的写法分享
2013/06/12 Javascript
JQuery表单验证插件EasyValidator用法分析
2014/11/15 Javascript
去除html代码里面的script正则方法
2016/05/19 Javascript
浅谈Javascript中的函数、this以及原型
2016/10/09 Javascript
微信小程序 wxapp内容组件 progress详细介绍
2016/10/31 Javascript
javascript中json基础知识详解
2017/01/19 Javascript
基于JS递归函数细化认识及实用实例(推荐)
2017/08/07 Javascript
简单了解JavaScript作用域
2020/07/31 Javascript
详细分析JavaScript中的深浅拷贝
2020/09/17 Javascript
[02:44]2014DOTA2 国际邀请赛中国区预选赛 大神红毯秀
2014/05/25 DOTA
python冒泡排序算法的实现代码
2013/11/21 Python
Python SQLAlchemy基本操作和常用技巧(包含大量实例,非常好)
2014/05/06 Python
有关wxpython pyqt内存占用问题分析
2014/06/09 Python
python自动化测试之从命令行运行测试用例with verbosity
2014/09/28 Python
Python数据结构之翻转链表
2017/02/25 Python
Python Grid使用和布局详解
2018/06/30 Python
Python根据服务获取端口号的方法
2019/09/25 Python
浅谈python的elementtree模块处理中文注意事项
2020/03/06 Python
Python3标准库之threading进程中管理并发操作方法
2020/03/30 Python
html5-canvas中使用clip抠出一个区域的示例代码
2018/05/25 HTML / CSS
英语系本科生个人求职信
2013/09/21 职场文书
自动化职业生涯规划书范文
2014/01/03 职场文书
2014党员学习《反腐倡廉警示教育读本》思想汇报
2014/09/13 职场文书
2015年财务试用期工作总结
2014/12/24 职场文书
Mysql数据库手动及定时备份步骤
2021/11/07 MySQL
Vue elementUI表单嵌套表格并对每行进行校验详解
2022/02/18 Vue.js
python通过新建环境安装tfx的问题
2022/05/20 Python