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? Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment 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 ... Quote Link to comment 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 )); }; Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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.