dreamwest Posted September 19, 2009 Share Posted September 19, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/174773-foreach-while-loop-is-sticking/ Share on other sites More sharing options...
sKunKbad Posted September 19, 2009 Share Posted September 19, 2009 why $matches[1]?, why not $matches Quote Link to comment https://forums.phpfreaks.com/topic/174773-foreach-while-loop-is-sticking/#findComment-921072 Share on other sites More sharing options...
trq Posted September 19, 2009 Share Posted September 19, 2009 for some reason the foreach is "sticking" after it matches the first link in the $page What are you doing with $link within the for exactly? Quote Link to comment https://forums.phpfreaks.com/topic/174773-foreach-while-loop-is-sticking/#findComment-921074 Share on other sites More sharing options...
dreamwest Posted September 19, 2009 Author Share Posted September 19, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/174773-foreach-while-loop-is-sticking/#findComment-921076 Share on other sites More sharing options...
trq Posted September 19, 2009 Share Posted September 19, 2009 Your re-assigning $matches within the for loop (at your call to preg_match). Quote Link to comment https://forums.phpfreaks.com/topic/174773-foreach-while-loop-is-sticking/#findComment-921078 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.