1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| function get_link($string,$inibase1=""){
$url=array();
$urltexte=array();
$urlext=array();
$len=strlen($string);
$pos=-1;
$debut=strtok($inibase1,"#?");
$inibase1=ereg("^http://[^/]*",$debut) ? $debut."/".substr($inibase1,strlen($debut)) : $inibase1;
$inibase2=ereg_replace("^(http://.+)/[^/]*$","\\1",
ereg_replace("^http://[/]*","http://",$debut))."/";
$inibase3=ereg_replace("^(http://[^/]+)/.*","\\1",$inibase2)."/";
$base1=$inibase1;$base2=$inibase2;$base3=$inibase3;
$nextbasepos=strpos($string,"<base");
while ((($pos=strpos($string,"<a ",$pos+1))!==false) &&
(($pos2=strpos($string,'>',$pos+1))!==false) &&
(($pos3=strpos($string,"<",$pos2+1))!==false)){
$str=substr($string,$pos,$pos3-$pos+1);
while (($nextbasepos!==false) && ($nextbasepos < $pos)){
if ((($pos4=strpos($string,">",$nextbasepos+1))!==false)
&& ereg('href[ ]*=[ ]*"[ ]*(http:[^"]*)"',substr($string,$nextbasepos,$pos4-$nextbasepos+1),$arr)){
$basex=trim($arr[1]);
$base1=((substr($basex,0,7)==="http://") ?
$basex :
((substr($basex,0,6)==="http:/") ?
$inibase3.substr($basex,6) :
$inibase2.substr($basex,5)));
$debut=strtok($base1,"#?");
$base1=ereg("^http://[^/]*",$debut) ? $debut."/".substr($base1,strlen($debut)) : $base1;
$base2=ereg_replace("^(http://.+)/[^/]*$","\\1",
ereg_replace("^http://[/]*","http://",$debut))."/";
$base3=ereg_replace("^(http://[^/]+)/.*","\\1",$base2)."/";
}
$nextbasepos=$pos4 ? strpos($string,"<base",$pos4) : false;
}
$pos=$pos3;
if (ereg('href[^"]*"[ ]*([^"]*)"[^>]*>([^<]*)<',$str,$arr)){
$http=(substr(trim($arr[1]),0,7)==="http://");
$root=(substr(trim($arr[1]),0,1)==="/");
$self= !trim($arr[1]) || (substr(trim($arr[1]),0,1)==="#");
array_push($url, ($link=($http ?
trim($arr[1]) :
($self ?
strtok($base1,'#').trim($arr[1]) :
($root ? $base3.substr(trim($arr[1]),1) :
$base2.trim($arr[1]))))));
array_push($urltexte,$arr[2]);
array_push($urlext,
ereg("[^?#]*[^/?#]/[a-zA-Z0-9_.\-]+(\.[a-zA-Z0-9]+)(\?|#|$)",$link,$arr2) ? $arr2[1] : "");
}
}
return array($url,$urltexte,$urlext);
} |
Partager