Page 1 of 1

for each loop not executing correctly with function call..

Posted: Thu Nov 27, 2008 5:14 am
by sruthim
I am trying to get the broken links for any web site. This is the code that I wrote for that

Code: Select all

 
<?php
 
function get_a_href($file)
{
    $h1count = preg_match_all('/(href=")(.*?)(")/i',$file,$patterns);
    return $patterns[2];
}
 
function get_images($file)
{
    $h1count = preg_match_all('/(<.*)\s (src="(.*)")/isxmU',$file,$patterns);
    $res = array();
    array_push($res,$patterns[3]);
    array_push($res,count($patterns[3]));
    return $res;
}
 
function pingLink($domain)
{
         $file= @fopen($domain,"r");
        $status= -1;
 
    if (!$file)
    {
       $status = -1;  
    }
    else
    {
        $status = 1;
        fclose($file);
    }
//echo"now returnong";
    return $status;
}
 
 
 function getBrokenLinks($url)
{
      $arrayOfLinks=array();
      $ind=0;
      $file = file_get_contents($url);
      $links = get_a_href($file);
      $images = get_images($file);
      
 
    if(!empty($links[0]))
        {
        echo "<ul>";
         echo "<br/>Links found:<ul>";
        foreach($links as $key => $val)
        {
        if($val!='#')
            {
               $p=strpos($val,'http');
               echo $ind;
               if($p>-1)
                {  
                   $arrayOfLinks[$ind++]=$val;
                  echo "<li>".$val."</li>";
                }
               else
                {
                    $arrayOfLinks[$ind++]=$url."/".$val;
                   echo "<li>".$url."/".$val."</li>";
                }
            }
        }
        echo "</ul>";
    }
    else
    {
        echo "<br/><div class=\"error\">No Links found</div><br/>";
    }
 
    
echo "<h1>Images</h1>";
echo "no if images==".$images[1];
    if(!empty($images[0]))
    {
        echo "</ul>";
        echo "<br/>images:<ul>";
        foreach($images[0] as $key => $val)
        {
            echo $ind;
            $p=strpos($val,'http');
               if($p>-1)
                {
            $arrayOfLinks[$ind++]=$val;
             echo "<li>".$val."</li>";
                }
               else
                {
                    $arrayOfLinks[$ind++]=$url."/".$val;
                   echo "<li>".$url."/".$val."</li>";
                }
        }
        echo "</ul>";
    }
    else
    {
        echo "<br/>No images found";
    }
    echo "value of ind before foreach==".$ind;
    
    $i=0;
    echo "<br>no of elts in arrayOfIndex arry===".count($arrayOfLinks);
    echo "<ul>";
    for($i=0;$i<count($arrayOfLinks);$i++)
    {
        $st=pingLink($arrayOfLinks[$i]);
        echo "<li>".$i."  ".$arrayOfLinks[$i]." ".$st."</li>";
    }
    //foreach($arrayOfLinks as $val)
    //{   
        //$st=pingLink($val);
        //echo "<li>".$i."  ".$val." ".$st;
        //if($st==1)
        //  echo "<img src='valid.png'></li>";
        //else
        //{if($st==-1)
       //   echo "<img src='invalid.png'></li>";}
 
        //$i++;
    //}
 echo "value of i after foreach==".$i;
 echo "</ul>";
}
 
$url='http://www.zycomm.net';
getBrokenLinks($url);
?>
 
But the array - $arrayOfLinks - length shows some 56 links, but when we call the pingLink function, it shows around 50 or 46 links at one time..Any guesses y??

Re: for each loop not executing correctly with function call..

Posted: Thu Nov 27, 2008 6:23 am
by pcoder
It takes a time to open these every files.
Increase the maximum execution time on php.ini file and check it out.
You can also use the set_time_limit function to limit the maximum execution time of the script.
Cheers

Re: for each loop not executing correctly with function call..

Posted: Thu Nov 27, 2008 10:50 pm
by sruthim
Hi,
Did that ...But no use...
Hmm... Should I take care of anything else?

Re: for each loop not executing correctly with function call..

Posted: Thu Nov 27, 2008 11:07 pm
by pcoder
These kind of script really takes time to execute.
I think it should be because of the internet speed and the maximum execution time.
I tried this code with different execution time and for the more execution time , it works.
Please check out the maximum execution time it takes to execute this script and the maximum execution time set on your server.
Cheers 8)