Modernvox Posted November 2, 2009 Share Posted November 2, 2009 Don't know why the foreach is invalid? <?php function curlURL($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2'); $output = curl_exec($curl); return $output; } $curlResults = curlURL("http://southcoast.craigslist.org/sss/"); $pattern = '#<a href="(/[a-z]{3}/\d{10}\.html)">#'; preg_match_all( $pattern, $curlResults, $matches); echo "<pre>\n"; echo "Links:\n\n"; foreach ($matches[1] as $link): echo "\t" . '<a href="' . $link . '" target="_BLANK">' . $link . '</a>' . "\n"; endforeach; echo '</pre>'; echo file_get_contents("http://southcoast.craigslist.org".$link); $pattern = '#<sale-[a-z0-9]+-\d+@craigslist\.org>#'; //This is the attempted match for the email preg_match_all( $pattern, $link, $matches); foreach ($matches[1] as $address): echo $address; $dbx= mysql_connect("localhost", "root", ""); //include before any database implematation if (!$dbx) { die('Could not connect: ' . mysql_error()); } mysql_select_db("craigslist", $dbx); mysql_query("INSERT INTO `addresses` (`sale_items`) VALUES ('$address')"); mysql_close($dbx); endforeach; ?> Quote Link to comment https://forums.phpfreaks.com/topic/179911-invalid-argument-supplied-for-foreach-in-cxampphtdocstest5php-on-line-27/ Share on other sites More sharing options...
Alex Posted November 2, 2009 Share Posted November 2, 2009 You get that when the first argument isn't an Array. Quote Link to comment https://forums.phpfreaks.com/topic/179911-invalid-argument-supplied-for-foreach-in-cxampphtdocstest5php-on-line-27/#findComment-949081 Share on other sites More sharing options...
Modernvox Posted November 2, 2009 Author Share Posted November 2, 2009 You get that when the first argument isn't an Array. So my REgex is off? Quote Link to comment https://forums.phpfreaks.com/topic/179911-invalid-argument-supplied-for-foreach-in-cxampphtdocstest5php-on-line-27/#findComment-949082 Share on other sites More sharing options...
Alex Posted November 2, 2009 Share Posted November 2, 2009 Seems like it, preform print_r() on $matches to see what it contains. Quote Link to comment https://forums.phpfreaks.com/topic/179911-invalid-argument-supplied-for-foreach-in-cxampphtdocstest5php-on-line-27/#findComment-949085 Share on other sites More sharing options...
corbin Posted November 2, 2009 Share Posted November 2, 2009 When ever you're not sure that an array will contain what you think it will, you should do: if(is_array($var)) { foreach... } To avoid that. Anyway, yeah, your regexp is a bit off. The first thing that I noticed is that you don't have any capturing parenthesis anywhere. Trying putting () around what ever you're trying to extract. Quote Link to comment https://forums.phpfreaks.com/topic/179911-invalid-argument-supplied-for-foreach-in-cxampphtdocstest5php-on-line-27/#findComment-949090 Share on other sites More sharing options...
Modernvox Posted November 2, 2009 Author Share Posted November 2, 2009 When ever you're not sure that an array will contain what you think it will, you should do: if(is_array($var)) { foreach... } To avoid that. Anyway, yeah, your regexp is a bit off. The first thing that I noticed is that you don't have any capturing parenthesis anywhere. Trying putting () around what ever you're trying to extract. ~<sale-([a-z0-9]+-\d+@craigslist\.org)~' ? Quote Link to comment https://forums.phpfreaks.com/topic/179911-invalid-argument-supplied-for-foreach-in-cxampphtdocstest5php-on-line-27/#findComment-949099 Share on other sites More sharing options...
Modernvox Posted November 2, 2009 Author Share Posted November 2, 2009 Any takers on this? Quote Link to comment https://forums.phpfreaks.com/topic/179911-invalid-argument-supplied-for-foreach-in-cxampphtdocstest5php-on-line-27/#findComment-949121 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.