I was working on an integration with Stripe payment processing gateway. The integration required that we retrieve prior payments from a customer and display them on the website. When Stripe returns the customer’s payment information, it also sends the currency that the payment was made in. However, it only sends the three digit currency code (i.e. usd, gbp, cad, etc.) along with the payment information. When displaying the amount that was paid, it would be nice to show the currency symbol instead of the three digit currency code like $10 instead of usd 10.
How to Get Currency Symbols from Three Digit Currency Code
In my case, I was using Stripe, but you can easily use this example on any number of other applications outside of Stripe.
function show_currency_symbol( $currency ) { $currencies = array('aed'=>'AED','afn'=>'؋','all'=>'Lek','amd'=>'AMD','ang'=>'ƒ','aoa'=>'AOA','ars'=>'$','aud'=>'$','awg'=>'ƒ','azn'=>'ман','bam'=>'KM','bbd'=>'$','bdt'=>'BDT','bgn'=>'лв','bhd'=>'BHD','bif'=>'BIF','bmd'=>'$','bnd'=>'$','bob'=>'$b','brl'=>'R$','bsd'=>'$','btn'=>'BTN','bwp'=>'P','byr'=>'p.','bzd'=>'BZ$','cad'=>'$','cdf'=>'CDF','chf'=>'CHF','clp'=>'$','cny'=>'¥','cop'=>'$','crc'=>'₡','cuc'=>'CUC','cup'=>'₱','cve'=>'CVE','czk'=>'Kč','djf'=>'DJF','dkk'=>'kr','dop'=>'RD$','dzd'=>'DZD','egp'=>'£','ern'=>'ERN','etb'=>'ETB','eur'=>'€','fjd'=>'$','fkp'=>'£','gbp'=>'£','gel'=>'GEL','ggp'=>'£','ghs'=>'¢','gip'=>'£','gmd'=>'GMD','gnf'=>'GNF','gtq'=>'Q','gyd'=>'$','hkd'=>'$','hnl'=>'L','hrk'=>'kn','htg'=>'HTG','huf'=>'Ft','idr'=>'Rp','ils'=>'₪','imp'=>'£','inr'=>'₹','iqd'=>'IQD','irr'=>'﷼','isk'=>'kr','jep'=>'£','jmd'=>'J$','jod'=>'JOD','jpy'=>'¥','kes'=>'KES','kgs'=>'лв','khr'=>'៛','kmf'=>'KMF','kpw'=>'₩','krw'=>'₩','kwd'=>'KWD','kyd'=>'$','kzt'=>'лв','lak'=>'₭','lbp'=>'£','lkr'=>'₨','lrd'=>'$','lsl'=>'LSL','lyd'=>'LYD','mad'=>'MAD','mdl'=>'MDL','mga'=>'MGA','mkd'=>'ден','mmk'=>'MMK','mnt'=>'₮','mop'=>'MOP','mro'=>'MRO','mur'=>'₨','mvr'=>'MVR','mwk'=>'MWK','mxn'=>'$','myr'=>'RM','mzn'=>'MT','nad'=>'$','ngn'=>'₦','nio'=>'C$','nok'=>'kr','npr'=>'₨','nzd'=>'$','omr'=>'﷼','pab'=>'B/.','pen'=>'S/.','pgk'=>'PGK','php'=>'₱','pkr'=>'₨','pln'=>'zł','prb'=>'PRB','pyg'=>'Gs','qar'=>'﷼','ron'=>'lei','rsd'=>'Дин.','rub'=>'руб','rwf'=>'RWF','sar'=>'﷼','sbd'=>'$','scr'=>'₨','sdg'=>'SDG','sek'=>'kr','sgd'=>'$','shp'=>'£','sll'=>'SLL','sos'=>'S','srd'=>'$','ssp'=>'SSP','std'=>'STD','syp'=>'£','szl'=>'SZL','thb'=>'฿','tjs'=>'TJS','tmt'=>'TMT','tnd'=>'TND','top'=>'TOP','try'=>'₺','ttd'=>'TT$','twd'=>'NT$','tzs'=>'TZS','uah'=>'₴','ugx'=>'UGX','usd'=>'$','uyu'=>'$U','uzs'=>'лв','vef'=>'Bs','vnd'=>'₫','vuv'=>'VUV','wst'=>'WST','xaf'=>'XAF','xcd'=>'$','xof'=>'XOF','xpf'=>'XPF','yer'=>'﷼','zar'=>'R','zmw'=>'ZMW'); if( array_key_exists( $currency, $currencies) ){ $symbol = $currencies[$currency]; } else { $symbol = $currency; } return $symbol; } |
What the code does is it allows you to pass a three digit currency code to the function. It then looks to see if the currency code you passed to the function exists in the array of $currencies. If it does, it will return the HTML code for that currency symbol. If it does not, it will simply return the same three digit code you passed to the function.
You would use it like this:
$currency = 'usd'; $symbol = show_currency_symbol( $currency ); echo $symbol; // this would display $, which is output by the HTML code $ |
You will notice in the array that not all currencies have an HTML symbol available. I did some searching to find currency symbols for all of those currency codes, and when nothing turned up, I resorted to using the uppercase version of the three digit currency code. If I missed anything, please let me know so I can update the code.
Why not just hard code a currency symbol?
When you are creating a plugin that can be used by someone anywhere in the world, you can’t predict what currency the plugin will ultimately need to return. Sure it’s easy enough to slap a Dollar sign next to the numeric output, but what if someone in England needs the Pound symbol, or someone in India needs the Rupee symbol? This code will allow your plugin to dynamically display the correct symbol depending on your user’s needs.
Leave a Reply