Modernvox Posted December 30, 2009 Share Posted December 30, 2009 Could you please explain the workings of using the foreach statement? Yes, I have spent much time reading on this. Maybe someone has the gift of communicating this to me in an easy to understand manner. Maybe a simple example would help? -I guess I can't comprehend why we need foreach() why can't we just print or echo the $matches[1]? -Why does the $matches only display one match when there are at least 50 on each page? $CL= Craigslist See.. I am attempting to scrape musician ads from CL, but leave the link intact leading back to CL. I get the information I need , but only one or two ads are scraped. Not only that , but the links are not linking back to CL and also the ads are being mirrored (2 in a row of the same. Here is my current code: <?php $html = file_get_contents("http://southcoast.craigslist.org/muc/"); preg_match_all('/<a href="([^"]+)">([^<]+)<\/a><font size="-1">([^"]+)<\/font>/s', $html,$posts); foreach ($posts as $post) { print $post[1]; // do something with data } ?> I think if someone explains the workings of the foreach function I can move on. As always, thanks guys! Link to comment https://forums.phpfreaks.com/topic/186740-please-read-and-take-a-second-to-explain-this-to-me/ Share on other sites More sharing options...
.josh Posted December 30, 2009 Share Posted December 30, 2009 http://www.phpfreaks.com/tutorial/php-loops Link to comment https://forums.phpfreaks.com/topic/186740-please-read-and-take-a-second-to-explain-this-to-me/#findComment-986140 Share on other sites More sharing options...
.josh Posted December 30, 2009 Share Posted December 30, 2009 and btw i think your problem is what array you are trying to loop through. preg_match_all returns a multi-dimensonal array, based on whether you have things being captured in your pattern (which you do). I suggest after your preg_match_all, you do this: echo "<pre>";print_r($posts); to get an idea of the structure of the array being returned. Link to comment https://forums.phpfreaks.com/topic/186740-please-read-and-take-a-second-to-explain-this-to-me/#findComment-986141 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.