前言
大家应该都有所体会,很多时候在做网络爬虫的时候特别需要将爬虫搜索到的超链接进行处理,统一都改成绝对路径的,所以本文就写了一个正则表达式来对搜索到的链接进行处理。下面话不多说,来看看详细的介绍吧。
通常我们可能会搜索到如下的链接:
index.html / target="_blank" target="_blank" / alt="超链接" target="_blank" title="超链接" / alt="超链接" / a /index.html?id=1 ?id=2 //index.html //www.mafutian.net http://www.php.cn/ http://www.php.cn/ http://www.php.cn/ 1.jpg 1.jpeg 1.gif 1.png 1.txt index.html index.html ./index.html ../index.html .../ ... javascript:void(0) a:b /a#a:b mailto:'mafutian@126.com' /tencent://message/?uin=335134463 . .. ../ /a/b/.. /a ./b ./././././././././b ../c ../../d ../a/../b/c/../d ./../e http://www.php.cn/ ./.././f http://www.php.cn/ :8081/index.html :80/index.html http://www.php.cn/:8081/index.html http://www.php.cn/:8082/index.html
处理的第一步,设置成绝对路径:
立即学习“PHP免费学习笔记(深入)”;
http:// ... / ../ ../
然后本文讲讲如何去除绝对路径中的 './'、'../'、'/..'的实现代码:
function url_to_absolute($relative)
{
$absolute = '';
// 去除所有的 './'
$absolute = preg_replace('/(?= 1);
// 除去最后的 '/..'
$absolute = preg_replace('/(?
更多PHP利用正则表达式将相对路径转成绝对路径的方法相关文章请关注PHP中文网!











