通常我们可能会搜索到如下的链接:
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.hole_1.com/index.html http://www.mafutian.net http://www.numberer.net 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.hole_1.org/./../e ./.././f http://www.hole_1.org/../a/.../../b/c/../d/.. :8081/index.html :80/index.html http://www.mafutian.net:8081/index.html http://www.mafutian.net:8082/index.html
处理的第一步,设置成绝对路径:
http:// ... / ../ ../
然后本文讲讲如何去除绝对路径中的 './'、'../'、'/..'的实现代码:
function url_to_absolute($relative)
{
$absolute = '';
// 去除所有的 './'
$absolute = preg_replace('/(?= 1);
// 除去最后的 '/..'
$absolute = preg_replace('/(?