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

Link to comment
Share on other sites

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
Share on other sites

$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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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