Zero-Affect Posted June 9, 2010 Share Posted June 9, 2010 function currency_format($amount, $currency='') { $currency_array = array( 'GBP' => '£', 'JPY' => '¥', 'CNY' => '¥', 'EUR' => '€', 'USD' => '$', '' => '' ); if ( (ctype_alpha($currency) OR empty($currency)) && array_key_exists($currency, $currency_array) && ctype_digit($amount) ) { $ret_txt = '<span style="font-weight: bold;">'.$currency_array[$currency].'</span>'.number_format($amount); } else { $ret_txt = FALSE; } return $ret_txt; } $var = '2001'; echo currency_format($var, 'USD'); // outputs $2,001 echo currency_format($var, 'GBP'); // outputs £2,001 echo currency_format($var, 'JPY'); // outputs ¥2,001 echo currency_format($var, 'CNY'); // outputs ¥2,001 echo currency_format($var, 'EUR'); // outputs €2,001 echo currency_format($var); // outputs 2,001 echo currency_format($var, 'lol'); // fails echo currency_format('+'.$var, 'USD'); // fails I was bored and needed something like this so i made it and thought i'd post my result Quote Link to comment Share on other sites More sharing options...
Dillion & Amanda 4 Lif Posted June 9, 2010 Share Posted June 9, 2010 An even nicer function could be exchange rates, since All the currencies have different rates. It'd be cool if you could grab each one from a site that updates them, but current ones are fine Quote Link to comment Share on other sites More sharing options...
Zero-Affect Posted June 9, 2010 Author Share Posted June 9, 2010 I actually was thinking about doing a exchange rate one but the exchange rates are updated all the time there's not much point really. Quote Link to comment Share on other sites More sharing options...
Dillion & Amanda 4 Lif Posted June 9, 2010 Share Posted June 9, 2010 You know any sites with API's that you can grab the current exchange rate from? Quote Link to comment Share on other sites More sharing options...
LordDan Posted June 9, 2010 Share Posted June 9, 2010 function exchangeRate( $amount, $currency, $exchangeIn ) { $googleQuery = $amount . ' ' . $currency . ' in ' . $exchangeIn; $googleQuery = urlEncode( $googleQuery ); $askGoogle = file_get_contents( 'http://www.google.com/search?q=' . $googleQuery ); $askGoogle = strip_tags( $askGoogle ); $matches = array(); preg_match( '/= (([0-9]|\.|,|\ )*)/', $askGoogle, $matches ); return $matches[1] ? $matches[1] : false; } echo exchangeRate( 1000, 'euro', 'dollars' ); This what you mean? Quote Link to comment Share on other sites More sharing options...
Zero-Affect Posted June 10, 2010 Author Share Posted June 10, 2010 never thought of it that way, thanks for that LordDan Quote Link to comment Share on other sites More sharing options...
Dayo Posted June 10, 2010 Share Posted June 10, 2010 i made a function like this its in the mccodes free mods it has the conversion rate too wops didnt see the auto updating Quote Link to comment Share on other sites More sharing options...
Karlos94 Posted June 10, 2010 Share Posted June 10, 2010 Why not do it a nice simple way? Using a regularly updated XML file with the latest exchange rates, they aren't that hard to find. ;) Quote Link to comment Share on other sites More sharing options...
LordDan Posted June 10, 2010 Share Posted June 10, 2010 Because I didn't write it, I just did a search and posted as reference ^^ I've never needed such a function myself.. Quote Link to comment Share on other sites More sharing options...
Karlos94 Posted June 10, 2010 Share Posted June 10, 2010 Oh I see, but the idea is there now, in case someone wishes to do it the way I said. 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.