博主辛苦了,我要打赏银两给博主,犒劳犒劳站长。
【摘要】很多时候在做网络爬虫的时候特别需要将爬虫搜索到的超链接进行处理,统一都改成绝对路径的,本文写一个正则表达式来对搜索到的链接进行处理。
通常我们可能会搜索到如下的链接:
<!-- 空超链接 -->
<a href=""></a>
<!-- 空白符 -->
<a href=" "> </a>
<!-- a标签含有其它属性 -->
<a href="index.html" alt="超链接"> index.html </a>
<a href="/" target="_blank"> / target="_blank" </a>
<a target="_blank" href="/" alt="超链接" > target="_blank" / alt="超链接" </a>
<a target="_blank" title="超链接" href="/" alt="超链接" > target="_blank" title="超链接" / alt="超链接" </a>
<!-- 根目录 -->
<a href="/"> / </a>
<a href="a"> a </a>
<!-- 含参数 -->
<a href="/index.html?id=1"> /index.html?id=1 </a>
<a href="?id=2"> ?id=2 </a>
<!-- // -->
<a href="//index.html"> //index.html </a>
<a href="//www.mafutian.net"> //www.mafutian.net </a>
<!-- 站内链接 -->
<a href="http://www.hole_1.com/index.html"> http://www.hole_1.com/index.html </a>
<!-- 站外链接 -->
<a href="http://www.mafutian.net"> http://www.mafutian.net </a>
<a href="http://www.numberer.net"> http://www.numberer.net </a>
<!-- 图片,文本文件格式的链接 -->
<a href="1.jpg"> 1.jpg </a>
<a href="1.jpeg"> 1.jpeg </a>
<a href="1.gif"> 1.gif </a>
<a href="1.png"> 1.png </a>
<a href="1.txt"> 1.txt </a>
<!-- 普通链接 -->
<a href="index.html"> index.html </a>
<a href="index.html"> index.html </a>
<a href="./index.html"> ./index.html </a>
<a href="../index.html"> ../index.html </a>
<a href=".../"> .../ </a>
<a href="..."> ... </a>
<!-- 非链接,含有链接冒号 -->
<a href="javascript:void(0)"> javascript:void(0) </a>
<a href="a:b"> a:b </a>
<a href="/a#a:b"> /a#a:b </a>
<a href="mailto:'mafutian@126.com'"> mailto:'mafutian@126.com' </a>
<a href="/tencent://message/?uin=335134463"> /tencent://message/?uin=335134463 </a>
<!-- 相对路径 -->
<a href="."> . </a>
<a href=".."> .. </a>
<a href="../"> ../ </a>
<a href="/a/b/.."> /a/b/.. </a>
<a href="/a"> /a </a>
<a href="./b"> ./b </a>
<a href="./././././././././b"> ./././././././././b </a> <!-- 其实就是 ./b -->
<a href="../c"> ../c </a>
<a href="../../d"> ../../d </a>
<a href="../a/../b/c/../d"> ../a/../b/c/../d </a>
<a href="./../e"> ./../e </a>
<a href="http://www.hole_1.org/./../e"> http://www.hole_1.org/./../e </a>
<a href="./.././f"> ./.././f </a>
<a href="http://www.hole_1.org/../a/.../../b/c/../d/.."> http://www.hole_1.org/../a/.../../b/c/../d/.. </a>
<!-- 带有端口号 -->
<a href=":8081/index.html"> :8081/index.html </a>
<a href="http://www.mafutian.net:80/index.html"> :80/index.html </a>
<a href="http://www.mafutian.net:8081/index.html"> http://www.mafutian.net:8081/index.html </a>
<a href="http://www.mafutian.net:8082/index.html"> 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 将相对路径转成绝对路径【正则表达式处理】》
本文链接地址:http://www.mafutian.com/231.html
转载请务必注明出处,小生将不胜感激,谢谢! 喜欢本文或觉得本文对您有帮助,请分享给您的朋友 ^_^
顶0
踩0
第 1 楼 夏日博客 2016-12-02 11:08:47 河南郑州
评论审核未开启 |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
||