Jump to content

file_get_contents loop problem


myarro

Recommended Posts

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

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

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 ...

$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 ));

};

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.