0

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

2
  • Why are you using a regular expression to parse this instead of $_GET? It's MUCH easier to use that. Commented Nov 26, 2013 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 Commented Nov 26, 2013 at 1:25

1 Answer 1

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);
}
1
  • 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. Commented Nov 26, 2013 at 1:34

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.