Miteshsach86 Posted October 14, 2010 Share Posted October 14, 2010 Hi Guys, I really hope one of you clever genious's can help me. Below is a code which I derived myself, it basically opens up the amazon website on to the page, scrapes everything in between the body tags and then it's supposed to filter out everything into <p></p> tags. But I'm getting the following error :-\ Warning: Invalid argument supplied for foreach() in /var/www/vhosts/...scrapeamazon.php on line 13 <?php $bbc = "http://www.amazon.co.uk/"; preg_match_all('/<body[^>]*>\s*(.*?)\s*<\/body>/is',file_get_contents($bbc), $bbcwebsite); $bodytags = $bbcwebsite[1]; if(empty($bodytags) || $bodytags == NULL) { $bbcbody = "Nothing here 1!"; } else { foreach ($bodytags as $line_num => $link) { $bbcbody = $link . "<br />\n"; preg_match_all('/<p[^>]*>\s*(.*?)\s*<\/p>/is', $bbcbody, $bbcp); $pcontent = $bbcp[1] . "<br />\n"; if(empty($pcontent) || $pcontent == NULL) { $bbcptag = "Nothing here 2!"; } else { foreach ($pcontent as $line_num => $ptag) { $bbcptag .= $ptag . "<br />\n"; } } } } ?> I would really appreciate someone's help and thank you in advance. M Link to comment https://forums.phpfreaks.com/topic/215867-php-foreach-error-pls-help/ Share on other sites More sharing options...
BlueSkyIS Posted October 14, 2010 Share Posted October 14, 2010 apparently $bodytags is not an array. try print_r($bodytags) to check. Link to comment https://forums.phpfreaks.com/topic/215867-php-foreach-error-pls-help/#findComment-1122154 Share on other sites More sharing options...
AbraCadaver Posted October 14, 2010 Share Posted October 14, 2010 You're concatenating a string "<br />\n" with an array $bbcp[1] which gives you a string $pcontent, which you can't operate on with foreach(). Link to comment https://forums.phpfreaks.com/topic/215867-php-foreach-error-pls-help/#findComment-1122155 Share on other sites More sharing options...
Miteshsach86 Posted October 14, 2010 Author Share Posted October 14, 2010 Thanks BlueSkyIS but that the error I was getting. However AbraCadaver my friend, you are a genious! Thank you! Link to comment https://forums.phpfreaks.com/topic/215867-php-foreach-error-pls-help/#findComment-1122164 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.