我为此写了一个函数
function proccessURL($ParentURL,$URL){
$parse_url=parse_url($URL);
if(@$parse_url['host']==""){
$Parent_URL=parse_url($ParentURL);
$path=explode("/",@$parse_url['path']);
$redirect=0;
$lkey=0;
$flag=false;
while(list($key,$val)=each($path)){
if($val==".." or $val=="." or $val=="..."){
$redirect++;
$lkey=$key;
$flag=true;
}else{
break;
}
}
if($flag){
$matches=explode("/",$Parent_URL['path']);
end($matches);
$b=each($matches);
$n=$b['key'];
$url='';
for($i=0;$i<$n-$redirect;$i++){
$url.=$matches[$i]."/";
}
for($i=$redirect+1;next($path);$i++){
$url.=$path[$i]."/";
}
rtrim($url,"/");
$parse_url['path']=$url;
}else{
$parse_url['path']="/".@$parse_url['path'];
}
}else{
$Parent_URL['scheme']=$parse_url['scheme'];
$Parent_URL['host']=$parse_url['host'];
}
//print_r($parse_url);
if(@$parse_url['query']!=""){
$parse_url['query']="?".@$parse_url['query'];
}
if(@$parse_url['fragment']!=""){
$parse_url['fragment']="#".@$parse_url['fragment'];
}
return $Parent_URL['scheme']."://".@$Parent_URL['host'].@$parse_url['path'].@$parse_url['query'].@$parse_url['fragment'];
}
此函数解决链接地址
样本:
$CorrectLink=proccessURL("http://www.sepidarcms.ir/kernel/","../plugin/1.php");
输出为“http://www.sepidarcms.ir/plugin/1.php” 现在您可以通过 preg_match_all 解析网址
$html="Your HTML Str";
$URL="Your HTML Page Link";
preg_match_all("/href=\"([^\"]*)\"/is", $html, $matches);
while(list($key,$val)=each($matches[1])){
$val=proccessURL($URL,$val);
echo $val;
}
此代码为您正确列出所有href Url