jcrichard Posted April 26, 2007 Share Posted April 26, 2007 I'm having a problem trying to use the foreach statement... 3: $filename = "http://www.website.com"; 4: $ch = curl_init(); 5: $timeout = 5; 6: curl_setopt ($ch, CURLOPT_URL, $filename); 7: curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 8: curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 9: $fcontents = curl_exec($ch); 10: curl_close($ch); 11: 12: foreach ($fcontents as $k $v) 13: { 14: if (ereg("STOP ON THIS LINE",$v)) { 15: $line=$k; 16: break; 17: } 18: else {} 19: } 20: 21: echo $fcontents[$line]; I get this error: Parse error: syntax error, unexpected T_VARIABLE, expecting ')' on line 12 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/48743-solved-foreach-statement/ Share on other sites More sharing options...
Barand Posted April 26, 2007 Share Posted April 26, 2007 Should be foreach ($fcontents as $k => $v) Quote Link to comment https://forums.phpfreaks.com/topic/48743-solved-foreach-statement/#findComment-238899 Share on other sites More sharing options...
jcrichard Posted April 26, 2007 Author Share Posted April 26, 2007 If I use ($fcontents as $k => $v) I get this: Warning: Invalid argument supplied for foreach() on line 12 Quote Link to comment https://forums.phpfreaks.com/topic/48743-solved-foreach-statement/#findComment-239084 Share on other sites More sharing options...
ToonMariner Posted April 26, 2007 Share Posted April 26, 2007 the curl execution failed or did not return an array. Quote Link to comment https://forums.phpfreaks.com/topic/48743-solved-foreach-statement/#findComment-239100 Share on other sites More sharing options...
jcrichard Posted April 26, 2007 Author Share Posted April 26, 2007 The curl execution did not fail... But reading the curl_exec docs, it says it returns a string, not an array... I just need to be able to go through each line returned to $fcontents to find the line I need... Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/48743-solved-foreach-statement/#findComment-239107 Share on other sites More sharing options...
jcrichard Posted April 26, 2007 Author Share Posted April 26, 2007 Fixed: $filename = "http://www.website.com"; $ch = curl_init(); $timeout = 5; curl_setopt ($ch, CURLOPT_URL, $filename); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $fcontents = curl_exec($ch); curl_close($ch); $array = explode("\n", $fcontents); foreach ($array as $k => $v) { if (ereg("STOP ON THIS LINE",$v)) { $line=$k; break; } else {} } echo $array[$line]; Quote Link to comment https://forums.phpfreaks.com/topic/48743-solved-foreach-statement/#findComment-239126 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.