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 相关文章推荐
WIN98下Apache1.3.14+PHP4.0.4的安装
Oct 09 PHP
phpMyAdmin 安装教程全攻略
Mar 19 PHP
phpmyadmin 常用选项设置详解版
Mar 07 PHP
通过5个php实例细致说明传值与传引用的区别
Aug 08 PHP
php实现批量压缩图片文件大小的脚本
Jul 04 PHP
老版本PHP转义Json里的特殊字符的函数
Jun 08 PHP
php实现比较全的数据库操作类
Jun 18 PHP
PHP中一个有趣的preg_replace函数详解
Aug 15 PHP
Laravel 5.5 异常处理 &amp; 错误日志的解决
Oct 17 PHP
laravel5 Eloquent 实现事务方式
Oct 21 PHP
TP5框架安全机制实例分析
Apr 05 PHP
PHP Trait功能与用法实例分析
Jun 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和AJAX创建RSS聚合器的代码
2007/03/13 PHP
成为好程序员必须避免的5个坏习惯
2014/07/04 PHP
PHP实现的迷你漂流瓶
2015/07/29 PHP
浅谈PHP链表数据结构(单链表)
2016/06/08 PHP
PHP+Apache环境中如何隐藏Apache版本
2017/11/24 PHP
关于取不到由location.href提交而来的上级页面地址的解决办法
2009/07/30 Javascript
JQuery+CSS提示框实现思路及代码(纯手工打造)
2013/05/07 Javascript
getAsDataURL在Firefox7.0下无法预览本地图片的解决方法
2013/11/15 Javascript
异步动态加载JS并运行(示例代码)
2013/12/13 Javascript
使用node.js半年来总结的 10 条经验
2014/08/18 Javascript
Jquery promise实现一张一张加载图片
2015/11/13 Javascript
JavaScript统计字符串中每个字符出现次数完整实例
2016/01/28 Javascript
jquery实现界面无刷新加载登陆注册
2016/07/30 Javascript
jquery把int类型转换成字符串类型的方法
2016/10/07 Javascript
把json格式的字符串转换成javascript对象或数组的方法总结
2016/11/03 Javascript
浅谈angular2的http请求返回结果的subcribe注意事项
2017/03/01 Javascript
PHP7新特性简述
2017/06/11 Javascript
Vue2 配置 Axios api 接口调用文件的方法
2017/11/13 Javascript
vue 界面刷新数据被清除 localStorage的使用详解
2018/09/16 Javascript
vue使用better-scroll实现下拉刷新、上拉加载
2018/11/23 Javascript
Vue.js样式动态绑定实现小结
2019/01/24 Javascript
关于layui toolbar和template的结合使用方法
2019/09/19 Javascript
python中偏函数partial用法实例分析
2015/07/08 Python
详解Python数据分析--Pandas知识点
2019/03/23 Python
python中bs4.BeautifulSoup的基本用法
2019/07/27 Python
python函数参数(必须参数、可变参数、关键字参数)
2019/08/16 Python
Pytorch中index_select() 函数的实现理解
2019/11/19 Python
python Protobuf定义消息类型知识点讲解
2021/03/02 Python
Canvas实现保存图片到本地的示例代码
2018/06/28 HTML / CSS
顶丰TOPPIK台湾官网:增发纤维假发,告别秃发困扰
2018/06/13 全球购物
一份软件工程师的面试试题
2016/02/01 面试题
卫校中专生个人自我评价
2013/09/19 职场文书
宗教学大学生职业生涯规划范文
2014/02/08 职场文书
党的群众路线教育实践活动查摆问题自查报告
2014/10/10 职场文书
党支部2014年度工作总结
2014/12/04 职场文书
毛主席纪念堂观后感
2015/06/17 职场文书