myarro Posted February 26, 2011 Share Posted February 26, 2011 I am using file_get_contents to grab HTML pages. Seems to work fine as a part of a 1-shot function. But as soon as I include the function as part of a loop, it doesn't return anything... for ($i = 0; $i < 10; $i++) { ... getFile($urlArray[$i]; ... } function getFile($whatURL) { return strtolower(file_get_contents( $whatURL )); } I originally had the strtolower(file_get_contents( $whatURL )); line in the for loop but thought there needed to be some sort of pause for the file_get_contents method to function, but neither seems to work. What's the deal? Link to comment https://forums.phpfreaks.com/topic/228913-file_get_contents-loop-problem/ Share on other sites More sharing options...
kenrbnsn Posted February 26, 2011 Share Posted February 26, 2011 You're not doing anything with the returned string. Instead of <?php getFile($urlArray[$i]); ?> You need to assign the value returned by the function to a variable so you can use it: <?php $some_var = getFile($urlArray[$i]); // // use $some_var... // ?> Ken Link to comment https://forums.phpfreaks.com/topic/228913-file_get_contents-loop-problem/#findComment-1179967 Share on other sites More sharing options...
myarro Posted February 26, 2011 Author Share Posted February 26, 2011 whoops. I do have that.. $file = getFile($urlArray[$i]); in the loop. Thanks. Any other ideas? Link to comment https://forums.phpfreaks.com/topic/228913-file_get_contents-loop-problem/#findComment-1179968 Share on other sites More sharing options...
kenrbnsn Posted February 26, 2011 Share Posted February 26, 2011 Please post the rest of your code. Ken Link to comment https://forums.phpfreaks.com/topic/228913-file_get_contents-loop-problem/#findComment-1179969 Share on other sites More sharing options...
dilbertone Posted February 26, 2011 Share Posted February 26, 2011 howdy myarro interesting thing - I am using file_get_contents to grab HTML pages. Seems to work fine as a part of a 1-shot function. But as soon as I include the function as part of a loop, it doesn't return anything... for ($i = 0; $i < 10; $i++) { ... getFile($urlArray[$i]; ... What's the deal? well i eagerly want to know how the full code will look like. I currently work on the same thing... Look forward to hear from you cheers db1 BTW - ever tried to do it with CURL ... Link to comment https://forums.phpfreaks.com/topic/228913-file_get_contents-loop-problem/#findComment-1179972 Share on other sites More sharing options...
myarro Posted February 26, 2011 Author Share Posted February 26, 2011 $newLinks is an array of URLs to be scraped $newLinkCount = count($newLinks); for ($k = 0; $k < $newLinkCount; $k++) { $file = getFile($pageURL); preg_match_all('/(<a.*?<\/a>)/',$file,$match,PREG_SET_ORDER); ... do a bunch of stuff with $match }; function getFile($whatFile) { return = strtolower(file_get_contents( $whatFile )); }; Link to comment https://forums.phpfreaks.com/topic/228913-file_get_contents-loop-problem/#findComment-1180024 Share on other sites More sharing options...
kenrbnsn Posted February 26, 2011 Share Posted February 26, 2011 How are you determining that it's not working? Ken Link to comment https://forums.phpfreaks.com/topic/228913-file_get_contents-loop-problem/#findComment-1180058 Share on other sites More sharing options...
myarro Posted February 26, 2011 Author Share Posted February 26, 2011 I placed echo in getFile function getFile($whatFile) { $xFile = strtolower(file_get_contents( $whatFile )); echo strlen($xFile); return = strtolower(file_get_contents( $whatFile )); }; The echo returns a string length of 0. Link to comment https://forums.phpfreaks.com/topic/228913-file_get_contents-loop-problem/#findComment-1180059 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.