I'm a novice at php and I'm trying to take a stock quote from the yahoo finance site and display it in a table of my own. As far as I know the following code should do it: <?php $data = file_get_contents("http://finance.yahoo.com/q?s=acpw"); $startData = strpos($data, "yfs_l10_acpw"); $startData = $startData + 2; echo "<p>$startData</p>"; $endData = strpos($data, "</span>", "$startData"); echo "<p>$endData</p>"; $length = $endData - $startData; $quote = substr($data, $startData, $length); echo "<p>$quote</p>"; ?> However, the "yfs_l10_acpw" is causing problems. It's a unique id for the table cell I'm interested in so it simplifies the whole procedure if I can use it but for some reason the underscores ("_") seem to be making a mess of things in the strpos(). I I simply type "acpw" or "yfs", etc then the strpos() works but if I type "_acpw" then $startData ends up being 2 everytime (basically it is adding 2 to 0 in line 4). I can't seem to find any reason why the underscore is a problem. Punctuation is supposed to be considered a piece of a string and the underscore is just punctuation. It never requires any escaping in other instances so I'm just at a loss as to why it hates me so. Any helpful ideas?
I looked through the source code of the yahoo url you are using, and I could find yfs_l10_acpw. It doesn't exist.
Yahoo is doing some sort of cloaking of the page. The html you get when you request the page through a browser (well at least Firefox) does include the span and id you're looking for. The html you get if you use wget to get the page or php file_get_contents does not. There's also a minor error in your code in that the first strpos will give you the position of the start of the string. You need to skip over the search string and the "> after it to get to what you want. $data = file_get_contents("http://finance.yahoo.com/q?s=acpw"); $needle = '<big>'; $startData = strpos($data, $needle); $startData = $startData + strlen($needle) + 2; echo "<p>$startData</p>\n"; $endData = strpos($data, "</b></big>", "$startData"); echo "<p>$endData</p>\n"; $length = $endData - $startData; $quote = substr($data, $startData, $length); echo "<p>$quote</p>\n"; Code (markup):