Jump to content

Foreach & while loop is Sticking


dreamwest

Recommended Posts

I have a while loop from the database and a foreach loop but theres something wacky going on cause it continually looping the first database result:

 

while($row = mysql_fetch_assoc( $result )){

$page = $row['page'];

preg_match_all('~<a\b[^>]+href\s?=\s?[\'"](.*?)[\'"]~is', $page, $matches);

foreach ($matches[1] as $link) {

$link = trim($link);
}
}//end while loop

 

So the $page might have 100 links in it, but for some reason the foreach is "sticking" after it matches the first link in the $page

 

 

Link to comment
https://forums.phpfreaks.com/topic/174773-foreach-while-loop-is-sticking/
Share on other sites

while($row = mysql_fetch_assoc( $result )){

$url = $row['url'];

// set URL and other appropriate options
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; da; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

$page = $content;

preg_match_all('~<a\b[^>]+href\s?=\s?[\'"](.*?)[\'"]~is', $page, $matches);

foreach ($matches[1] as $link) {

$link = trim($link);

//check the page size//
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
  echo 'cURL failed';
  exit;
}

$contentLength = 'unknown';
$status = 'unknown';
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
  $contentLength = (int)$matches[1];
}
$filesize = round(($contentLength / 1224)) ;

}
}//end while loop

 

Thats pretty much it, the rest is just getting the meta tags an inserting into a database

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.