2013-02-28 34 views
0

我使用preg_match_all其返回該錯誤:preg_match_all返回一個錯誤未定義,偏移

Notice: Undefined offset: 0 in B:\xampp\htdocs\fogsy\link_searcher.php on line 98 

,這是第98行:

$server_name=$matches[0][1]."/"; 

這裏是我的功能: 它是用來檢索鏈接來自html正文。

function GetLinks($body_str,$parent_url) 
     {   
      $url_list=array(); 

      preg_match_all('/http:\/\/(.*)\//iU', $parent_url, $matches, PREG_SET_ORDER); 
      $server_name=$matches[0][1]."/"; 

      preg_match_all('/< *a.*href *= *[\'"](.*)[\'"].*>(.*)< *\/a *>/iU', $body_str, $matches, PREG_SET_ORDER); 

      for($count=0;$count<count($matches);$count++) 
       { 
        $text=$matches[$count][2]; 

        if(strpos(strtolower($matches[$count][1]),"http://")===false&&strpos(strtolower($matches[$count][1]),"www")===false) 
        $href="http://".$server_name.trim($matches[$count][1],"/");       
        else $href=$matches[$count][1];        

        $url_list[$text."_".$count]=$href; 
       } 
      return $url_list; 
     } 

有什麼想法嗎?

回答

1

這是監守你沒有一個指標0瀏覽:$server_name=$matches[0][1]."/";

的print_r($比賽);看看。

它的發生是因爲沒有在$parent_url

更改像這樣的匹配模式:

if(count($matches) > 0){ 
    $server_name=$matches[0][1]."/"; 
} 
else{ 
    $server_name=""; 
} 

但是,這會影響你的其他功能,更好的發送以及形成$parent_url這個功能。

+0

哦。疑難雜症。謝謝 – cppit 2013-02-28 07:14:44

+0

@fogsy:酷:D – 2013-02-28 07:15:29