0

I have a problem, I want to get data from another website and display it on my website using cURL.

For example, this is data from the other website which I would like to parse and display on my website.

<div class="users-name"> <a href='#'>User 1</a></div>
<div class="users-name"> <a href='#'>User 2</a></div>
<div class="users-name"> <a href='#'>User 3</a></div>
<div class="users-name"> <a href='#'>User 4</a></div>
<div class="users-name"> <a href='#'>User 5</a></div>
<div class="users-name"> <a href='#'>User 6</a></div>
<div class="users-name"> <a href='#'>User 7</a></div>
<div class="users-name"> <a href='#'>User 8</a></div>

Currently I'm getting first row of data, but now I want to get all of this data.
I have tried this so far:

$curl = curl_init('www.domain.com/search.php');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

$page = curl_exec($curl);

if(curl_errno($curl)) // check for execution errors
{
    echo 'Scraper error: ' . curl_error($curl);
    exit;
}

curl_close($curl);

$regex = '/<div class="users-name">(.*?)<\/div>/s';

if(preg_match($regex, $page, $list))
    print_r($list[0]);
else
    print "Not found";
2
  • What do you mean? You are explicitly calling first match in the list with print_r($list[0]); Commented Apr 2, 2014 at 15:07
  • If you change print_r($list[0]); to print_r($list); you should see the entire contents of the array. It should contain the other matches. Commented Apr 2, 2014 at 15:08

1 Answer 1

1

Replace

preg_match($regex, $page, $list)

with

preg_match_all($regex, $page, $list)

preg_match stops after the first match. preg_match_all gets all the matches.

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

2 Comments

@user3487256 in StackOverflow if you find answer correct please vote up and accept it as correct answer.
i have one more problem, can i get more than one div in one time. for example: <div class="users-name"> <a href='#'>User 1</a></div> i'm getting all this now i also want user address. its another div <div class="users-name-address"> <a href='#'>users 1 address</a></div>

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.