[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 381
  • Last Modified:

Retrieving results from a page's <div></div> container fails

Hi

I want to retrieve results from a site, of which I am a member of, and therefore want to display the results, well look at the code please, it returns "array()" instead of the data i want.

<?php
$scrape_address = "http://competitie.knbb.nl/overview.php?CompID=2011";
$ch = curl_init($scrape_address);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, '1');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_ENCODING, "");
$data = curl_exec($ch);
preg_match('% <div class="ranking">(.*)</div> %six', $data, $match);
print_r($match);
?>

Open in new window


http://www.webshophelp.nl/test1.php
0
PeterdeB
Asked:
PeterdeB
  • 3
  • 2
1 Solution
 
Julian HansenCommented:
preg_match returns an array for matches - that is what it does.

The first match will be in $match[1]

Is that what you are looking for?

PS. The link does not work - gives 404

However your regex is not formed correctly  you are enclosing your ranking in double quotes when the actual returned value is in single quotes.

Working on a fix ...
0
 
PeterdeBAuthor Commented:
Sorry fixed the link and changed the code but it does not display anything now

<?php

$scrape_address = "http://competitie.knbb.nl/overview.php?CompID=2011";
$ch = curl_init($scrape_address);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, '1');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_ENCODING, "");
$data = curl_exec($ch);
preg_match('% <div class="ranking">(.*)</div> %six', $data, $match);

print_r($match[0]);

?>

Open in new window


But it returns 0, which means there is no match, so then the regex will be faulty right?
0
 
Julian HansenCommented:
Here you go - this should work - your regex needed some modification - see second text area for value you are looking for

<?php
$regx = '/<div class=\'ranking\'>(.*)<\/div>?/msU';
$scrape_address = "http://competitie.knbb.nl/overview.php?CompID=2011";
$ch = curl_init($scrape_address);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, '1');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_ENCODING, "");
$data = curl_exec($ch);
preg_match($regx, $data, $match);
echo "<textarea>";
print_r($match);
echo "</textarea>";
// This is what you want
echo "<textarea>";
echo $match[1];
echo "</textarea>";
?>

Open in new window

0
 
PeterdeBAuthor Commented:
Fast response, clear explanation great many thanks
0
 
Julian HansenCommented:
You are welcome.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now