Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
Why are you using a regular expression to parse this instead of $_GET? It's MUCH easier to use that. –  Machavity Nov 26 '13 at 1:20
    
I'm sorry mate, I forgot, the function I'm created is to extract link from page and the page have pagination. Assume the page have 5 pages, So I only get contents 5 times and save all URL in "$newarray" array. Thanks –  Aby Nov 26 '13 at 1:25

1 Answer 1

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);
}
share|improve this answer
    
Thanks, I found the answer I just need array_merge function @Mike : yea, the pagination I take from the original page, because they use some "key" to identify the page ex : someserver.com/content.php?page=2&key=789234697d if(strpos($result,'Next Page')) This is just a sample filter to know the next page is available or not, if available, It will process another code to get the "next url". Thanks Mike and Machavity for fast response. Really appreciate it. –  Aby Nov 26 '13 at 1:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.