这个php用户定义函数有什么问题吗?

发布于 2024-12-05 19:37:25 字数 2336 浏览 0 评论 0原文

我找不到我在这个函数中错过的东西,所以它有时会产生这个错误,但并不总是如此?

错误:-

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;  
        }

I cant find that what i have missed in this function so its generating this ERROR some time not always ?

ERROR :-

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

Line number 131 is defined in the below code.

        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;  
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如日中天 2024-12-12 19:37:25

将行更改为:

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; 
}

Change the line to:

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

...and the error should disappear.

EDIT here is an edited version of the function with that error avoided, and a couple of pointless variables removed, and a couple of lines concatenated, and a missing { added.

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; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文