jskarr1982 Posted March 12, 2011 Share Posted March 12, 2011 Hello! I am working on a script to fetch a price of metal, remove the dollar sign, and then multiple or add it by a number. My current code I created is as follows: <?php $file = file_get_contents("http://www.apmex.com"); $do = preg_match('/<span id="ctl08_ctl05_gvMetals_ctl02_label3">(.*)<\/span>/', $file, $matches); if ($do = true) { $no_dollar = preg_replace('/[\$]/', '', $matches); print $no_dollar['1']; } else { echo "No Price"; } ?> I got the script to pull a current value and then remove the dollar sign, however I need to multiply it by a value (Say, 1.05). I've tried for several days without anything more than a 0 result. Any suggestions? Thank you so much, Jeremy Quote Link to comment https://forums.phpfreaks.com/topic/230444-web-fetch-remove-multiply-variable/ Share on other sites More sharing options...
sasa Posted March 12, 2011 Share Posted March 12, 2011 before calculate with price you must strip out comma too change regex pattern in preg replace from [\$] to [\$,] <?php $file = file_get_contents("http://www.apmex.com"); $do = preg_match('/<span id="ctl08_ctl05_gvMetals_ctl02_label3">(.*)<\/span>/', $file, $matches); if ($do = true) { $no_dollar = preg_replace('/[\$,]/', '', $matches); print $no_dollar['1']; } else { echo "No Price"; } echo "\n<hr />Price multypled with 1.7 is ",$no_dollar[1]*1.7; ?> Quote Link to comment https://forums.phpfreaks.com/topic/230444-web-fetch-remove-multiply-variable/#findComment-1186717 Share on other sites More sharing options...
.josh Posted March 12, 2011 Share Posted March 12, 2011 fyi you should be doing if ($do == true) { = is assignment operator, == is equality operator. Quote Link to comment https://forums.phpfreaks.com/topic/230444-web-fetch-remove-multiply-variable/#findComment-1186733 Share on other sites More sharing options...
jskarr1982 Posted March 12, 2011 Author Share Posted March 12, 2011 @ Crayon Violent and sasa, thank you so much! I've been spending the past days pulling my hair out trying to figure this out. I got it to work with multiplication and addition, but how do I get it to show as such: $1,111.11. Where it has a dollar sign, comma, and only two denominators? Thank you so much for getting me to this point! Jeremy Quote Link to comment https://forums.phpfreaks.com/topic/230444-web-fetch-remove-multiply-variable/#findComment-1186760 Share on other sites More sharing options...
jskarr1982 Posted March 13, 2011 Author Share Posted March 13, 2011 2 decimals, not denominators; sorry! Quote Link to comment https://forums.phpfreaks.com/topic/230444-web-fetch-remove-multiply-variable/#findComment-1186847 Share on other sites More sharing options...
DavidAM Posted March 13, 2011 Share Posted March 13, 2011 Use number_format() Quote Link to comment https://forums.phpfreaks.com/topic/230444-web-fetch-remove-multiply-variable/#findComment-1186860 Share on other sites More sharing options...
jskarr1982 Posted March 13, 2011 Author Share Posted March 13, 2011 Hello, Using the numbers format I got to the following which doesn't work: <?php $file = file_get_contents("http://www.apmex.com"); $do = preg_match('/<span id="ctl08_ctl05_gvMetals_ctl02_label3">(.*)<\/span>/', $file, $matches); if ($do = true) { $no_dollar = preg_replace('/[\$,]/', '', $matches); print $no_dollar['1']; $dollar = $no_dollar['1']*1.02; // english notation without thousands seperator $english_format_number = number_format($dollar, 2, '.', ','); // 1234.57 print $english_format_number['1']; } else { echo "No Price"; } echo "\n<hr />Price multiplied with 1.02 is ",$no_dollar[1]*1.02; ?> Thank you, Jeremy Quote Link to comment https://forums.phpfreaks.com/topic/230444-web-fetch-remove-multiply-variable/#findComment-1186909 Share on other sites More sharing options...
sasa Posted March 13, 2011 Share Posted March 13, 2011 try <?php $file = file_get_contents("http://www.apmex.com"); $do = preg_match('/<span id="ctl08_ctl05_gvMetals_ctl02_label3">(.*)<\/span>/', $file, $matches); if ($do) { $no_dollar = preg_replace('/[\$,]/', '', $matches); $dollar = $no_dollar['1']*1.02; echo "Price multiplied with 1.02 is ", number_format($dollar,2,'.',','); } else { echo "No Price"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/230444-web-fetch-remove-multiply-variable/#findComment-1186913 Share on other sites More sharing options...
jskarr1982 Posted March 14, 2011 Author Share Posted March 14, 2011 This works out perfectly! Thank you so much for all the help. Quote Link to comment https://forums.phpfreaks.com/topic/230444-web-fetch-remove-multiply-variable/#findComment-1187226 Share on other sites More sharing options...
CrazyPig83 Posted April 3, 2011 Share Posted April 3, 2011 Hi jskarr1982, I'm working on a website which is very similar with you. I just need the silver spot price but i cant seem to get the codes correct. Anyone can help me out abit here ? im losing hair fast >.< <?php $file = file_get_contents("http://www.apmex.com/Category/503/Silver.aspx"); $do = preg_match('/<span id="ctl10_ctl00_MetalInfo1_lblAsk">(.*)<\/span>/', $file, $matches); if ($do) { $no_dollar = $matches; $dollar = $no_dollar['1']; echo $dollar; } else { echo "No Price"; } ?> <?php echo $dollar; ?> I'm get No Price output but i had double check the span id and the url and the source code...its all correct =( Thanks, Victor Quote Link to comment https://forums.phpfreaks.com/topic/230444-web-fetch-remove-multiply-variable/#findComment-1196300 Share on other sites More sharing options...
sasa Posted April 3, 2011 Share Posted April 3, 2011 it echo 37.91 for me Quote Link to comment https://forums.phpfreaks.com/topic/230444-web-fetch-remove-multiply-variable/#findComment-1196319 Share on other sites More sharing options...
CrazyPig83 Posted April 4, 2011 Share Posted April 4, 2011 Hi Sasa, Thanks for testing it for me....strangely im still getting no value in return. Do you happen to know what could be the cause ? Thank you, Victor Quote Link to comment https://forums.phpfreaks.com/topic/230444-web-fetch-remove-multiply-variable/#findComment-1196472 Share on other sites More sharing options...
dcro2 Posted April 4, 2011 Share Posted April 4, 2011 Worked for me too. Maybe your host blocks outbound connections. Have you checked to see if file_get_contents actually returns anything? Quote Link to comment https://forums.phpfreaks.com/topic/230444-web-fetch-remove-multiply-variable/#findComment-1196476 Share on other sites More sharing options...
CrazyPig83 Posted April 4, 2011 Share Posted April 4, 2011 Hi dcro2, Ya...i think its actually server issue if you guys can get it to work. Thanks for helping out guys...i will check with my server provider. Thanks, Victor Quote Link to comment https://forums.phpfreaks.com/topic/230444-web-fetch-remove-multiply-variable/#findComment-1196480 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.