2011-09-22 116 views
0

我無法找到我在這個函數中遺漏了什麼,所以它在某些時候並不總是生成這個ERROR?這個php用戶定義函數有什麼問題?

ERROR: -

Notice: Undefined offset: 13 in /home/chandrak/public_html/tmp/c/Crawler.php on line 131 

行號131在下面的代碼所定義。

 function crawlImage($url) 
     { 
      $content=$this->getContent($url); 
      $domain=$this->getDomain($url); 

      //echo $domain,'<br>'; 
      $dom = new DOMDocument(); 
      @$dom->loadHTML($content);  
      $xdoc = new DOMXPath($dom); 
      //Read the images that is between <a> tag 
      $atags = $xdoc ->evaluate("//a");  //Read all a tags 
      $index=0; 

      for ($i = 0; $i < $atags->length; $i++) 
      { 
       $atag = $atags->item($i);   //select an a tag 
       $imagetags=$atag->getElementsByTagName("img");//get img tag 
       $imagetag=$imagetags->item(0); 
       if(sizeof($imagetag)>0)//if img tag exists 
       { 
        $imagelinked['src'][$index]=$imagetag->getAttribute('src');//save image src 
        $imagelinked['link'][$index]=$atag->getAttribute('href');//save image link  
        $index=$index+1; 
       } 
      }   
      //Read all image 
      //Betweem <img> tag 
      $imagetags = $xdoc ->evaluate("//img"); //Read all img tags 
      $index=0; 
      $indexlinked=0; 
      for ($i = 0; $i < $imagetags->length; $i++) 
      { 
       $imagetag = $imagetags->item($i);         
       $imagesrc=$imagetag->getAttribute('src');   
       $image['link'][$index]=null; 

    /*LINE NO 131 */ if($imagesrc==$imagelinked['src'][$indexlinked]) //THIS IS LINE NUBER 131   
          { 
        $image['link'][$index]=$this->convertLink($domain,$url,$imagelinked['link'][$indexlinked]); 
        $indexlinked=$indexlinked+1; 
       } 
       $image['src'][$index]=$this->convertLink($domain,$url,$imagesrc); 
       $index=$index+1;    
      }  
      return $image; 
     } 
+0

錯誤表示您嘗試訪問未定義的數組索引。 – Robik

+0

未定義的偏移警告只是表示在該索引位置沒有值。注意這不是一個錯誤,它只是一個通知,您可能想要關閉該頁面。 – Chibuzo

+0

@Chibuzo,或者不要關閉它,修復它!錯誤處理很重要。 :) –

回答

1

行更改爲:

if (isset($imagelinked['src'][$indexlinked]) && $imagesrc == $imagelinked['src'][$indexlinked]) { 

...和錯誤應該消失。

編輯這裏是與該錯誤避免功能的編輯版本,並刪除了幾個毫無意義的變量,和一對夫婦級聯線,以及缺少{增加。

function crawlImage ($url) { 
    $domain = $this->getDomain($url); 
    //echo $domain,'<br>'; 
    $dom = new DOMDocument(); 
    @$dom->loadHTML($this->getContent($url)); 
    $xdoc = new DOMXPath($dom); 
    // Read the images that is between <a> tag 
    $atags = $xdoc->evaluate("//a"); // Read all <a> tags 
    for ($index = 0, $i = 0; $i < $atags->length; $i++) { 
    $atag = $atags->item($i); // Select an <a> tag 
    $imagetags = $atag->getElementsByTagName("img");//get img tag 
    $imagetag = $imagetags->item(0); 
    if (sizeof($imagetag) > 0) { // If <img> tag exists 
     $imagelinked['src'][$index] = $imagetag->getAttribute('src'); // Save image src 
     $imagelinked['link'][$index] = $atag->getAttribute('href'); // Save image link 
     $index++; 
    } 
    } 
    // Read all images between <img> tag 
    $imagetags = $xdoc->evaluate("//img"); //Read all img tags 
    for ($indexlinked = 0, $i = 0; $i < $imagetags->length; $i++) { 
    $imagetag = $imagetags->item($i);   
    $imagesrc = $imagetag->getAttribute('src'); 
    $image['link'][$i] = NULL; 
    if (isset($imagelinked['src'][$indexlinked]) && $imagesrc == $imagelinked['src'][$indexlinked]) { 
     $image['link'][$i] = $this->convertLink($domain,$url,$imagelinked['link'][$indexlinked]); 
     $indexlinked++; 
    } 
    $image['src'][$i] = $this->convertLink($domain,$url,$imagesrc); 
    } 
    return $image; 
} 
+0

它現在很好,DaveRandom :) – Bajrang