What wrong with this php user defined function?
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 = $ata开发者_如何学Gogs->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;
}
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;
}
精彩评论