前言

大家应该都有所体会,很多时候在做网络爬虫的时候特别需要将爬虫搜索到的超链接进行处理,统一都改成绝对路径的,所以本文就写了一个正则表达式来对搜索到的链接进行处理。下面话不多说,来看看详细的介绍吧。
通常我们可能会搜索到如下的链接:
<!-- 空超链接 --> <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中相对路径的问题与绝对路径的使用
# PHP文件操作之获取目录下文件与计算相对路径的方法
# php命令行(cli)下执行PHP脚本文件的相对路径的问题解决方法
# php计算两个文件相对路径的方法
# PHP获取文件相对路径的方法
# php求两个目录的相对路径示例(php获取相对路径)
# 一道求$b相对于$a的相对路径的php代码
# php绝对路径与相对路径之间关系的的分析
# php zend 相对路径问题
# php 算法之实现相对路径的实例
# 超链接
# 站内
# 来看看
# 写了
# 这篇文章
# 谢谢大家
# 多说
# 设置成
# 端口号
# 迭代
# 有疑问
# alt
# target
# _blank
# html
# external
# nofollow
# index
# mafutian
相关文章:
javascript基本数据类型及类型检测常用方法小结
如何访问已购建站主机并解决登录问题?
如何通过虚拟主机快速完成网站搭建?
建站之星在线客服如何快速接入解答?
Thinkphp 中 distinct 的用法解析
,如何利用word制作宣传手册?
建站主机服务器选型指南与性能优化方案解析
建站之星各版本价格是多少?
c++怎么使用类型萃取type_traits_c++ 模板元编程类型判断【方法】
建站168自助建站系统:快速模板定制与SEO优化指南
宝塔建站教程:一键部署配置流程与SEO优化实战指南
mc皮肤壁纸制作器,苹果平板怎么设置自己想要的壁纸我的世界?
巅云智能建站系统:可视化拖拽+多端适配+免费模板一键生成
如何撰写建站申请书?关键要点有哪些?
开源网站制作软件,开源网站什么意思?
非常酷的网站设计制作软件,酷培ai教育官方网站?
logo在线制作免费网站在线制作好吗,DW网页制作时,如何在网页标题前加上logo?
如何快速搭建二级域名独立网站?
浙江网站制作公司有哪些,浙江栢塑信息技术有限公司定制网站做的怎么样?
简历在线制作网站免费,免费下载个人简历的网站是哪些?
北京营销型网站制作公司,可以用python做一个营销推广网站吗?
公司网站建设制作费用,想建设一个属于自己的企业网站,该如何去做?
独立制作一个网站多少钱,建立网站需要花多少钱?
已有域名和空间如何快速搭建网站?
如何在Golang中指定模块版本_使用go.mod控制版本号
如何高效利用200m空间完成建站?
如何通过可视化优化提升建站效果?
招贴海报怎么做,什么是海报招贴?
如何选购建站域名与空间?自助平台全解析
香港服务器网站搭建教程-电商部署、配置优化与安全稳定指南
专业商城网站制作公司有哪些,pi商城官网是哪个?
专业网站制作服务公司,有哪些网站可以免费发布招聘信息?
专业制作网站的公司哪家好,建立一个公司网站的费用.有哪些部分,分别要多少钱?
如何在云主机快速搭建网站站点?
5种Android数据存储方式汇总
兔展官网 在线制作,怎样制作微信请帖?
微信小程序制作网站有哪些,微信小程序需要做网站吗?
如何快速选择适合个人网站的云服务器配置?
家庭建站与云服务器建站,如何选择更优?
如何通过免费商城建站系统源码自定义网站主题与功能?
c++ stringstream用法详解_c++字符串与数字转换利器
建站之星IIS配置教程:代码生成技巧与站点搭建指南
自助网站制作软件,个人如何自助建网站?
c++如何打印函数堆栈信息_c++ backtrace函数与符号名解析【方法】
如何快速生成高效建站系统源代码?
建站主机选哪家性价比最高?
上海网站制作网站建设公司,建筑电工证网上查询系统入口?
制作国外网站的软件,国外有哪些比较优质的网站推荐?
c# Task.ConfigureAwait(true) 在什么场景下是必须的
邀请函制作网站有哪些,有没有做年会邀请函的网站啊?在线制作,模板很多的那种?
*请认真填写需求信息,我们会在24小时内与您取得联系。