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调用三种数据库的方法(2)
Oct 09 PHP
PHP5 安装方法
Jan 15 PHP
用header 发送cookie的php代码
Mar 16 PHP
PHP获取当前文件所在目录 getcwd()函数
May 13 PHP
PHP中全面阻止SQL注入式攻击分析小结
Jan 30 PHP
php 表单提交大量数据发生丢失的解决方法
Mar 03 PHP
thinkphp在低版本Nginx 下支持PATHINFO的方法分享
May 27 PHP
php 在字符串指定位置插入新字符的简单实现
Jun 28 PHP
PHP mysqli_free_result()与mysqli_fetch_array()函数详解
Sep 21 PHP
php 二维数组快速排序算法的实现代码
Oct 17 PHP
PHP实现随机数字、字母的验证码功能
Aug 01 PHP
thinkphp5 路由分发原理
Mar 18 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
便携利器 — TECSUN PL-365简评
2021/03/02 无线电
在PHP中使用与Perl兼容的正则表达式
2006/11/26 PHP
php xml留言板 xml存储数据的简单例子
2009/08/24 PHP
用PHP读取flv文件的播放时间长度
2009/09/03 PHP
php目录操作函数之获取目录与文件的类型
2010/12/29 PHP
php利用反射实现插件机制的方法
2015/03/14 PHP
PHP实现简单数字分页效果
2015/07/26 PHP
WordPress的主题编写中获取头部模板和底部模板
2015/12/28 PHP
浅析PHP中的i++与++i的区别及效率
2016/06/15 PHP
关于php几种字符串连接的效率比较(详解)
2017/02/22 PHP
PHP实现动态获取函数参数的方法示例
2018/04/02 PHP
PHP网页缓存技术优点及代码实例
2020/07/29 PHP
JS+CSS实现电子商务网站导航模板效果代码
2015/09/10 Javascript
js+css简单实现网页换肤效果
2015/12/29 Javascript
Javascript将JSON日期格式化
2016/08/23 Javascript
详解vue 模版组件的三种用法
2017/07/21 Javascript
jquery在启动页面时,自动加载数据的实例
2018/01/22 jQuery
Vue下滚动到页面底部无限加载数据的示例代码
2018/04/22 Javascript
浅谈JavaScript闭包
2019/04/09 Javascript
详解element-ui设置下拉选择切换必填和非必填
2019/06/17 Javascript
[03:28]2014DOTA2国际邀请赛 走近EG战队天才中单Arteezy
2014/07/12 DOTA
Python中pygame安装方法图文详解
2015/11/11 Python
使用Python做定时任务及时了解互联网动态
2019/05/15 Python
Python 依赖库太多了该如何管理
2019/11/08 Python
pandas factorize实现将字符串特征转化为数字特征
2019/12/19 Python
pytorch点乘与叉乘示例讲解
2019/12/27 Python
opencv中图像叠加/图像融合/按位操作的实现
2020/04/01 Python
在jupyter notebook中调用.ipynb文件方式
2020/04/14 Python
python的help函数如何使用
2020/06/11 Python
详解Django中views数据查询使用locals()函数进行优化
2020/08/24 Python
css3实现可拖动的魔方3d效果
2019/05/07 HTML / CSS
澳大利亚免息网上购物:Shop Zero
2016/09/17 全球购物
《我为你骄傲》教学反思
2014/02/20 职场文书
教师业务培训方案
2014/05/01 职场文书
2014年服务员个人工作总结
2014/12/23 职场文书
2016大学生形势与政策心得体会
2016/01/12 职场文书