php取頁面全部鏈接兼容GBK和utf8

最近要用到採集程序,需要取出整個頁面的鏈接,網上找到的一遇到中文鏈接就取不出完整的網址,自己改了一個,留個檔

 

<?php

function GetAllLink($string)
{
 $r_=chr(0xa1);
 $e_=chr(0xff);
 //echo "<br>".$e_."<br>";
   $string = str_replace("\r","",$string);
   $string = str_replace("\n","",$string);
  
   $regex[url] = "((http|https|ftp|telnet|news):\/\/)?([a-z0-9_\-\/\.]+\.[][a-z0-9:;&#@=_~%\?\/\.\,\+\-]+[a-z0-9_\-\=]+[\u4e00-\u9fa5]+[\xa0-\xff]{0,})";
   $regex[email] = "([a-z0-9_\-]+)@([a-z0-9_\-]+\.[a-z0-9\-\._\-]+)";  

   $string = eregi_replace(">[^<>]+<","><", $string);       

   $string = eregi_replace("<!--.*//-->","", $string);
 
   $string = eregi_replace("<[^a][^<>]*>","", $string);
      
   $string = eregi_replace("<a([ ]+)href=([\"']*)mailto:($regex[email])([\"']*)[^>]*>","", $string);
 
   $string = eregi_replace("<a([ ]+)href=([\"']*)($regex[url])([\"']*)[^>]*>","\\3\t", $string);

   $output[0] = strtok($string, "\t");
   while(($temp = strtok("\t")))
   {
     if($temp && !in_array($temp, $output))
       $output[++$i] = $temp;
   }

   return $output;
}

?>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章