Aby
Aby

Reputation: 67

PHP Array in looping function

I have php function to get item from some page, the item have pagination

function get_content($link){
    $string = file_get_contents($link);
    $regex = '/https?\:\/\/[^\" ]+/i';
    preg_match_all($regex, $string, $matches);

    //this is important
    foreach($matches as $final){
        $newarray[] = $final;
    }

    if(strpos($string,'Next Page')){ //asumme the pagination is http://someserver.com/content.php?page=2
        get_content($link);
    }

    return $newarray;
} 

Question :

  1. Is it possible to using looping function for that case?

  2. When I try it, why I only get 1 page of array? I mean if there is 5 page and each page have 50 links, I only get 50 links when I try to print_r the result, not 250.

Thank you

Upvotes: 0

Views: 78

Answers (1)

Mike Brant
Mike Brant

Reputation: 71424

You never set your recursive values into the main array you are building. And you are not changing $link at all in order to change the file you are getting data from.

You would need to do something like:

if(strpos($result,'Next Page')){ //asumme the pagination is http://someserver.com/content.php?page=2
    $sub_array = get_content($link . '?page=x'); // you need some pagination identifier probably
    $newarray = array_merge($new_array, $sub_array);
}

Upvotes: 1

Related Questions