nofx1728 Posted August 31, 2011 Share Posted August 31, 2011 I've used a little PHP but I am not great. Is there any way to take information from a html page written in basic text and have it dynamically displayed. The content I want is found here: http://www.kitco.com/texten/texten.html I want to take the information from the following chart - only Metal and Ask and create a ticker. New York Spot Price MARKET IS OPEN Will close in 2 hour 24 minutes ---------------------------------------------------------------------- Metals Bid Ask Change Low High ---------------------------------------------------------------------- Gold 1822.80 1823.80 -12.30 -0.67% 1810.70 1840.90 Silver 41.45 41.55 +0.10 +0.24% 41.08 42.15 Platinum 1844.00 1852.00 -7.00 -0.38% 1826.00 1861.00 Palladium 783.00 788.00 +10.00 +1.29% 775.00 794.00 ---------------------------------------------------------------------- So basically I would just take the word: Gold - $1823.80 Silver - $41.55 Platinum $1852.00 Palladium - $788.00 Can someone help me through this or point me in a direction where I can read more about accomplishing this? I've been searching google for articles but still haven't found what I'm looking for. nofx1728 Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted August 31, 2011 Share Posted August 31, 2011 How about this? <?PHP $content = file_get_contents('http://www.kitco.com/texten/texten.html'); $content = explode("\n",$content); for($i=39; $i<=42; $i++) { $thisLine = explode(' ',preg_replace('/(?:\s\s+|\n|\t)/', ' ',$content[$i]),4); $prices[$thisLine[1]] = '$'.$thisLine[2]; } echo '<pre>'; print_r($prices); echo '</pre>'; ?> It will return something like this: Array ( [Gold] => $1824.90 [silver] => $41.60 [Platinum] => $1843.00 [Palladium] => $782.00 ) Regards, PaulRyan. Quote Link to comment Share on other sites More sharing options...
nofx1728 Posted September 1, 2011 Author Share Posted September 1, 2011 That is awesome. Is there anywhere I can go to read more about what is taking place in that line of code? How exactly are you selecting only those numbers with all that information on the page? Is there anyway to change how it is outputted? I'm trying to create a finished product like this: <font><marquee border="1" width="400" height="20" bgcolor="#000000" scrolldelay="10" scrollamount="1" align="middle" style="background-color: #000000; color: #FFFFFF; font-weight: bold;>Gold: $1,824. Silver: $41.60 Platinum: $1,843.00 Palladium: $782.00 </marquee></font> I thought maybe removing the \n would accomplish the line break but I tested that and it didn't work. Either way thank you so much for your help. Just getting to this point is great. Quote Link to comment Share on other sites More sharing options...
nofx1728 Posted September 1, 2011 Author Share Posted September 1, 2011 I modified the code a little and used an if statement instead of a for statement - coming up with this code. <?PHP $content = file_get_contents('http://www.kitco.com/texten/texten.html'); $content = explode("\n",$content); if($i=39) { $thisLine = explode(' ',preg_replace('/(?:\s\s+|\n|\t)/', ' ',$content[$i]),4); $prices[$thisLine[1]] = '$'.$thisLine[2]; } if($i=40) { $thisLine = explode(' ',preg_replace('/(?:\s\s+|\n|\t)/', ' ',$content[$i]),4); $prices1[$thisLine[1]] = '$'.$thisLine[2]; } if($i=41) { $thisLine = explode(' ',preg_replace('/(?:\s\s+|\n|\t)/', ' ',$content[$i]),4); $prices2[$thisLine[1]] = '$'.$thisLine[2]; } if($i=42) { $thisLine = explode(' ',preg_replace('/(?:\s\s+|\n|\t)/', ' ',$content[$i]),4); $prices3[$thisLine[1]] = '$'.$thisLine[2]; } ?> Then I add it to my html that I want to display: <font><marquee border="1" width="400" height="20" bgcolor="#000000" scrolldelay="10" scrollamount="1" align="middle" style="background-color: #000000; color: #FFFFFF; font-weight: bold;"><?PHP print_r($prices);?> <?PHP print_r($prices1);?> <?PHP print_r($prices2);?> <?PHP print_r($prices3);?> </font> Now the only thing I need to do is figure out a way to remove the Array ( [] ). Is there anyway to just display Gold - $xxxxx - instead of Array ([Gold] => xxxx) ?? Any help would that would be unreal. I've been googling for last 4 hours but can't seem to figure it out. Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted September 4, 2011 Share Posted September 4, 2011 There was no reason to change any of the code, adding a simple foreach() is sufficient. <?PHP $content = file_get_contents('http://www.kitco.com/texten/texten.html'); $content = explode("\n",$content); for($i=39; $i<=42; $i++) { $thisLine = explode(' ',preg_replace('/(?:\s\s+|\n|\t)/',' ',$content[$i],3)); $prices[$thisLine[1]] = '$'.$thisLine[2]; } ?> <div style="background:#000; color:#FFF; font-weight:bold; width:400px;"> <marquee scrolldelay="10" scrollamount="1"> <?PHP foreach($prices AS $metal => $price) { echo "{$metal}: {$price} \n"; } ?> </marquee> </div> Regards, PaulRyan. Quote Link to comment Share on other sites More sharing options...
nofx1728 Posted September 6, 2011 Author Share Posted September 6, 2011 Awesome. Thank you so much. This is so much cleaner then what I had come up with to get it to work. Thanks again for all your help. Quote Link to comment Share on other sites More sharing options...
nofx1728 Posted September 22, 2011 Author Share Posted September 22, 2011 Hi, sorry to come back once again with another question on this issue but I just discovered a new problem. Is there anyway to create a statement that will display the above information if it gathers it correctly, but not display anything at all if I get an error retrieving the information? Right now I get a large code error on my screen that is throwing my layout off. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
mangy1983 Posted September 22, 2011 Share Posted September 22, 2011 I had help from the good guys on this forum myself with a similar problem. What you want to do is called screen scraping. I'm only posting some advice which l found out the hard way. I would have the data saved to a database as the way you you want it coded it will screen scrape from the http://www.kitco.com/texten/texten.html every time the page is loaded. This not only slows your page from loading whilst it retrieves the information but if the kitcos server admin sees all of your servers access to their server for the information you want to display they could block you especially if you have 100's of people loading up your page regularly. You could run your script once a day/hour etc using a cron job and save required data to your database. just my two cents worth cheers Callum Quote Link to comment Share on other sites More sharing options...
xyph Posted September 22, 2011 Share Posted September 22, 2011 This is the hokey way to do it, but it works and is easy. <?php // use @ to suppress errors. if( @file_get_contents('http://yourwebsite.com/page.html') ) { // success } else { // could not get page } ?> The right way changes the way you handle errors rather than suppressing them using something like set_error_handler Quote Link to comment Share on other sites More sharing options...
nofx1728 Posted September 23, 2011 Author Share Posted September 23, 2011 Thanks for your help everyone. You make a good point mangy I'm going to do some additional research on that and in the short term use the @ symbol. Quote Link to comment Share on other sites More sharing options...
xyph Posted September 23, 2011 Share Posted September 23, 2011 If you use the @ symbol, make sure the function returns what you want it to If you don't output some sort of message letting you know the function isn't working, you can't debug it. That's why I included an if/else in my example. 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.