0

A rather simple scenario; I've got an array of user inputted url's (could be any number from 1 to 1000+), and I want to perform file_get_contents(); on all of them, and then if possible have all that binded/bound into a single variable, so that preg_match_all(); can be performed on that variable to pick up specific strings.

I've heard that using cURL might be another option, however I've got very little knowledge on the function of cURL.

2 Answers 2

3

Actually this sounds like a job for iterating over the URLs:

$urls = array('http://www.example.com/');
$allTexts = '';
foreach($urls as $url)
{
    $text = file_get_contents($url);
    if (false === $text)
        continue;

    // proceed with your text, e.g. concatinating it:
    $allTexts .= $text;
}

However if you have thousand URLs, take your time. curl offers to request multiple URLs at once (multi request feature) however, with thousands of URLs that does not scale as well.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, exactly what I'm looking for. Any idea how I'd go about concatenating $text? I don't really understand what's returned by foreach?
@Fireworksable: foreach does not actually return something like a function. Instead everything you do inside the code-block is done, e.g. if you set a variable, after the block it will be set. I updated the answer with a buffer for all texts named $allTexts.
1

Use array_map to apply function to every element of array

implode('',array_map('file_get_contents',$array));

1 Comment

This is a very simplified, fantastic answer. Personally, I would have never even thought of doing that! Learn something new every day. Up voted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.