2011-09-22 3 views
0

나는이 함수에서 빠져 나온 것을 발견하지 못한다.이 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]) { 

... 그리고 오류가 사라집니다. 여기

EDIT{는 추가 회피하는 오차 함수의 편집 된 버전 제거 무의미한 변수 쌍과 연결된 선을 연결하고, 누락된다.

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

관련 문제