JamieThompson90 Posted December 15, 2007 Share Posted December 15, 2007 Hello evryone, I hate to open a new account and ask a question straight off, but however I've done it and freely ashamed of myself! What it is, Im trying to capture Stock Quotes from Yahoo Finance so I came up with this code: <?php <?php $symbol='NRK.L'; $theurl="http://uk.finance.yahoo.com/q?s=$symbol"; if (!($contents = file_get_contents($theurl))) { echo 'Invalid URL'; exit; } $pattern = ' (((At\s([1-9]|1[0-2])[0-5][0-9])([AP]M))|(On (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s[0-9]+)\s</small><big><b>([0-9]+).([0-9]+)\sp)) '; if (eregi($pattern, $contents, $quote)) { echo "<p>"; echo "$quote[1] $symbol"; echo '</p>'; } else { echo '<p>No quote available</p>'; }; ?> In order to match the source code of http://uk.betastreaming.finance.yahoo.com/q?s=nrk.l Were it reads: <small>At 4:35PM </small><big><b>86.00 p OR <small>On Dec 14 </small><big><b>91.90 p This data is dynamic and will change regularly, the time will update (At x:xxAM|PM) Or it will display a date (On MMM DD) And the price will vary constantly. When I have checked my REGEX statment it works agains that specific string, however it willnot work agains the full page! Any ideas where I may be going wrong? I apricaite any help! Thanks in advanced. Jamie Quote Link to comment Share on other sites More sharing options...
dsaba Posted December 15, 2007 Share Posted December 15, 2007 this works for me: <?php $hay = '<small>at 4:35PM </small><big><b>86.00 p lal ala <small>On Dec 14 </small><big><b>91.90 p '; $pat '~<small>[a-zA-Z]{2} (.+?)</small><big><b>([0-9][0-9]\.[0-9][0-9]) p~s'; preg_match_all($pat, $hay, $out); print_r($out); ?> you were missing a delimeter in the pattern, and adding the s modifier will make it parse through multiple lines where ~ is the delimeter $pat = '~regex pattern~s'; you also might want to output your full source code page that you're using as your haystack, to make sure it is what you think it is Quote Link to comment Share on other sites More sharing options...
JamieThompson90 Posted January 1, 2008 Author Share Posted January 1, 2008 After looking into the preg_match function and more regex patterns I wrewrote my own code to solve my problem. Thanks for the helping hand! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.