View Full Version : Need help on scraping data off a webpage using preg.match and cURL

10-20-2012, 08:43 PM
I am trying to echo a specific piece of variable data off a webpage and I cannot manage to do it (page is http://www.myfxbook.com/members/NickMcDonald/pound-dollar-profits/282668). I can either echo the entire webpage, the links or the images, but not a specific variable off the page. The data I want is the +20.77%

<li class="paddB5 borderB"><span class="floatLeft dotted" id="absGainTip">Abs. Gain:</span>
<span class="floatNone"><span class='green'>+20.77%</span></span></li>

The code that I found that came closest was a pregmatch where I could find different parts of the title, body etc, but I only want the 'abs gain'


$start = '<title>';

$end = '</title>';

$start = preg_quote($start, '/');
$end = preg_quote($end, '/');

$url = 'http://www.myfxbook.com/members/NickMcDonald/pound-dollar-profits/282668';

$file = fopen("$url", "r");

$document = fread($file, 80000);

preg_match("/$start(.*?)$end/", $document, $matches);

echo ($matches);

Can anyone help me alter the code so that I can get what I need?:)

10-20-2012, 09:43 PM
I got an better approach:

#1 Use DOMdocument to load the html
#2 Use XPath query to get spesific html tag.

Note: this is just an example code I used 5min to create. So you need to tweak it.


$url = 'http://www.myfxbook.com/members/NickMcDonald/pound-dollar-profits/282668';

# DOMdocument
$dom = new DOMDocument();

# XPath
$xpath = new DOMXPath($dom);
$tags = $xpath->query('//ul[@class="quickStats"]/li/span/span[@class="green"]');
$arr = array();
foreach ($tags as $tag) {
$arr[] = htmlentities($tag->nodeValue);


Output: Array ( [0] => +20.77% [1] => +20.77% [2] => $415.38 )

10-20-2012, 10:16 PM
Just a question: Why do you not just use the API instead?

Since my code uses more than 2 seconds to get your result.