Jump to content



Photo

Regex for Yahoo Finance Stock Price


  • Please log in to reply
1 reply to this topic

#1 generalt

generalt

    Neowinian

  • Joined: 09-May 07

Posted 22 September 2012 - 14:12

I am trying to grab stock quotes from Yahoo Finance. I am completely new to regular expressions...

The stock price comes like this: <span id="yfs_l84_$symbol">price</span>, where $symbol is the ticker symbol and price is something like 6.75 (no $ sign)

This is what I have so far:


$content = file_get_contents('http://finance.yahoo.com/q?s=$symbol');

preg_match('#<span id="yfs_184_$symbol">#', $content, $match);
$price = $match[1];

echo "Price: $price";

I really have no idea what I'm doing with preg_match, and the PHP documentation didn't really help much. When I run this code, I get "error: Undefined Offset: 1," referring to $match[1].

What am I doing wrong?


#2 AnthonySterling

AnthonySterling

    Offering bad advice since 23-December 04.

  • Joined: 23-December 04
  • Location: North-East, UK

Posted 23 September 2012 - 10:31

Have you tried the CSV feed via YQL interface, it's pretty handy.

http://y.ahoo.it/k34ae

Note the REST link towards the bottom of the page.



Click here to login or here to register to remove this ad, it's free!