PHP 将相对路径转成绝对路径【正则表达式处理】

学习笔记 马富天 2016-12-02 09:37:36 136 1

【摘要】很多时候在做网络爬虫的时候特别需要将爬虫搜索到的超链接进行处理,统一都改成绝对路径的,本文写一个正则表达式来对搜索到的链接进行处理。

通常我们可能会搜索到如下的链接:

  1. <!-- 空超链接 -->
  2. <a href=""></a> 
  3. <!-- 空白符 -->
  4. <a href=" "> </a>
  5. <!-- a标签含有其它属性 -->
  6. <a href="index.html" alt="超链接"> index.html </a>
  7. <a href="/" target="_blank"> / target="_blank" </a>
  8. <a target="_blank" href="/" alt="超链接" > target="_blank" / alt="超链接" </a>
  9. <a target="_blank" title="超链接" href="/" alt="超链接" > target="_blank" title="超链接"	 / alt="超链接" </a>
  10. <!-- 根目录 -->
  11. <a href="/"> / </a>
  12. <a href="a"> a </a>
  13. <!-- 含参数 -->
  14. <a href="/index.html?id=1"> /index.html?id=1 </a>
  15. <a href="?id=2"> ?id=2 </a>
  16. <!-- // -->
  17. <a href="//index.html"> //index.html </a>
  18. <a href="//www.mafutian.net"> //www.mafutian.net </a>
  19. <!-- 站内链接 -->
  20. <a href="http://www.hole_1.com/index.html"> http://www.hole_1.com/index.html </a>
  21. <!-- 站外链接 -->
  22. <a href="http://www.mafutian.net"> http://www.mafutian.net </a>
  23. <a href="http://www.numberer.net"> http://www.numberer.net </a>
  24. <!-- 图片,文本文件格式的链接 -->
  25. <a href="1.jpg"> 1.jpg </a>
  26. <a href="1.jpeg"> 1.jpeg </a>
  27. <a href="1.gif"> 1.gif </a>
  28. <a href="1.png"> 1.png </a>
  29. <a href="1.txt"> 1.txt </a>
  30. <!-- 普通链接 -->
  31. <a href="index.html"> index.html </a>
  32. <a href="index.html"> index.html </a>
  33. <a href="./index.html"> ./index.html </a>
  34. <a href="../index.html"> ../index.html </a>
  35. <a href=".../"> .../ </a>
  36. <a href="..."> ... </a>
  37. <!-- 非链接,含有链接冒号 -->		
  38. <a href="javascript:void(0)"> javascript:void(0) </a>
  39. <a href="a:b"> a:b </a>
  40. <a href="/a#a:b"> /a#a:b </a>
  41. <a href="mailto:'mafutian@126.com'"> mailto:'mafutian@126.com' </a>
  42. <a href="/tencent://message/?uin=335134463"> /tencent://message/?uin=335134463 </a>		
  43. <!-- 相对路径 -->
  44. <a href="."> . </a>
  45. <a href=".."> .. </a>
  46. <a href="../"> ../ </a>
  47. <a href="/a/b/.."> /a/b/.. </a>
  48. <a href="/a"> /a </a>
  49. <a href="./b"> ./b </a>
  50. <a href="./././././././././b"> ./././././././././b </a> <!-- 其实就是 ./b -->
  51. <a href="../c"> ../c </a>
  52. <a href="../../d"> ../../d </a>
  53. <a href="../a/../b/c/../d"> ../a/../b/c/../d </a>
  54. <a href="./../e"> ./../e </a>
  55. <a href="http://www.hole_1.org/./../e"> http://www.hole_1.org/./../e </a>		
  56. <a href="./.././f"> ./.././f </a>
  57. <a href="http://www.hole_1.org/../a/.../../b/c/../d/.."> http://www.hole_1.org/../a/.../../b/c/../d/.. </a>	
  58. <!-- 带有端口号 -->
  59. <a href=":8081/index.html"> :8081/index.html </a>
  60. <a href="http://www.mafutian.net:80/index.html"> :80/index.html </a>
  61. <a href="http://www.mafutian.net:8081/index.html"> http://www.mafutian.net:8081/index.html </a>
  62. <a href="http://www.mafutian.net:8082/index.html"> http://www.mafutian.net:8082/index.html </a>

处理的第一步,设置成绝对路径:

  1. http:// ... / ../ ../

然后本文讲讲如何去除绝对路径中的 './'、'../'、'/..'的实现代码:

  1. function url_to_absolute($relative)
  2. {
  3. 	$absolute = '';
  4. 	//	去除所有的 './'
  5. 	$absolute = preg_replace('/(?<!\.)\.\//','',$relative);
  6. 	$count = preg_match_all('/(?<!\/)\/([^\/]{1,}?)\/\.\.\//',$absolute,$res);
  7. 	//	迭代去除所有的 '/abc/../'
  8. 	do
  9. 	{
  10. 		$absolute = preg_replace('/(?<!\/)\/([^\/]{1,}?)\/\.\.\//','/',$absolute);
  11. 		$count = preg_match_all('/(?<!\/)\/([^\/]{1,}?)\/\.\.\//',$absolute,$res);			
  12. 	}while($count >= 1);
  13. 	//	除去最后的 '/..'
  14. 	$absolute = preg_replace('/(?<!\/)\/([^\/]{1,}?)\/\.\.$/','/',$absolute);
  15. 	$absolute = preg_replace('/\/\.\.$/','',$absolute);
  16. 	//	除去存在的 '../'
  17. 	$absolute = preg_replace('/(?<!\.)\.\.\//','',$absolute);
  18. 	return $absolute;
  19. }
  20. $relative = 'http://www.mytest.org/../a/.../../b/c/../d/..';
  21. var_dump(url_to_absolute($relative));
  22. //	输出:string 'http://www.mytest.org/a/b/' (length=26)

版权归 马富天个人博客 所有

本文标题:《PHP 将相对路径转成绝对路径【正则表达式处理】》

本文链接地址:http://www.mafutian.com/231.html

转载请务必注明出处,小生将不胜感激,谢谢! 喜欢本文或觉得本文对您有帮助,请分享给您的朋友 ^_^

0

0

上一篇《 php 性能优化:使用 isset() 判断字符串长度速度比 strlen() 更快 》 下一篇《 PHP header 函数设置 404 状态 》

所有评论

  1. 首页
  2. 上一页
  3. 1
  4. 下一页
  5. 尾页
  6. 第1页
  7. 每页12条
  8. 共1页
  9. 共1条
评论审核未开启
表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情
验证码

TOP10

  • 浏览最多
  • 评论最多