Using file_get_contents to get web site data with regex

I am trying to get some ticket symbols from a Yahoo Finance site. Here is the code I have been using to try and scrape the data. My $matches array is returning nothing.

Any ideas?

<?php
$url = "http://finance.yahoo.com/gainers?e=aq";
$raw = file_get_contents($url);

$newlines = array("\	","\
","\\r","\\x20\\x20","\\0","\\x0B");
$content = str_replace($newlines, "", html_entity_decode($raw));

$start = strpos($content,'<div id="yfitp" class="yfitabsc">');
$end = strpos($content,'<div id="yfisrtq">',$start) + 18;
$content = substr($content,$start,$end-$start);

//set search pattern (using regular expressions)
$find = '|<td class="first"><b><a href="/q?s=.*?">(.*?)</a></b></td>|is';
preg_match_all($find, $content, $matches);
print_r($matches);
?>

Have you looked into YQL ? It provides access to at least some of Yahoo’s finance data.

Yes, it is fetching the site content. The issue seems to be when I am trying to use my search pattern to find the $matches for the ticker symbols.

First off, is file_get_contents() actually fetching anything? (you have not said).

file_get_contents() to another domain can sometimes be disabled by your server being in safe_mode or some other restriction.

To prove it try:


<?php

$url = "http://finance.yahoo.com/gainers?e=aq";
$raw = file_get_contents($url);
var_dump( $raw );
?>

If it comes up false then look in your error log file.

If you cannot use file_get_contents then look at trying cURL instead.

[edit]Oooh, I get a badge!
(“We don’t need no stinkin’ badges” - name that movie(s).)
[/edit]

I have.

I can’t seem to find any information on how to get the largest price gainers / losers for a single day.

This might be useful;