
PHP Currency Converter
Here is another PHP code snippet from my library. This PHP function allows you to calculate exchange amounts between 49 countries and 33 currencies.
Function is based on the live conversion reference rates provided by European Central Bank.
Usage
- First 3 parameters are required. First one is the origin currency and the second one is target currency.
- First 2 parameters should be the same type. If you are doing a county code based conversion both must be country codes. If you are doing currency code based conversion, both should be currency codes.
- 3rd parameter is the amount you want to be converted. Should be decimal digits without any currency signs or codes attached to it.
- 4th parameter is optional and set as true by default. When enabled function will prepend or append the currency sign (or code) to the output amount. You may want to turn that off if you will use the returned amount in a calculation.
- 5th and the last parameter is calculation precision. Controls the number of digits after the decimal point. Default is 5.
Usage Samples
- currency_convert(“EUR”,”USD”,100)
- currency_convert(“FR”,”GB”,98);
- currency_convert(“AUD”,”CNY”,145,false,8);
- currency_convert(“GR”,”DK”,3500,true,2);
Download
- Download PHP code snippet
- Code is PHP5 compatible but it should also work on PHP4 (Untested)
License
This code is free to use, distribute, modify and study. When referencing please link back to this website / post in any way e.g. direct link, credits etc. If you find this useful, please leave a comment and share using the buttons below!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
<?php // Realtime currency converter by Pixel Envision (E.Gonenc) // Version 1.0 - 18 August 2011 // Based on the live conversion reference rates provided by European Central Bank // http://www.ecb.europa.eu/ function currency_convert($from, $to, $amount, $sign = true, $sensitivity = 5) { $RATES = $BASE = $in = $out = $append = NULL; //Array of available countries & currencies $CURRENCY = array( "US" => "USD", "BE" => "EUR", "ES" => "EUR", "LU" => "EUR", "PT" => "EUR", "DE" => "EUR", "FR" => "EUR", "MT" => "EUR", "SI" => "EUR", "IE" => "EUR", "IT" => "EUR", "NL" => "EUR", "SK" => "EUR", "GR" => "EUR", "CY" => "EUR", "AT" => "EUR", "FI" => "EUR", "JP" => "JPY", "BG" => "BGN", "CZ" => "CZK", "DK" => "DKK", "EE" => "EEK", "GB" => "GBP", "HU" => "HUF", "LT" => "LTL", "LV" => "LVL", "PL" => "PLN", "RO" => "RON", "SE" => "SEK", "CH" => "CHF", "NO" => "NOK", "HR" => "HRK", "RU" => "RUB", "TR" => "TRY", "AU" => "AUD", "BR" => "BRL", "CA" => "CAD", "CN" => "CNY", "HK" => "HKD", "ID" => "IDR", "IN" => "INR", "KR" => "KRW", "MX" => "MXN", "MY" => "MYR", "NZ" => "NZD", "PH" => "PHP", "SG" => "SGD", "TH" => "THB", "ZA" => "ZAR" ); if (strlen($from) == 2 && strlen($to) == 2) { //Operate using country code if (isset($CURRENCY[$from])) { $in = $CURRENCY[$from]; } if (isset($CURRENCY[$to])) { $out = $CURRENCY[$to]; } } elseif (strlen($from) == 3 && strlen($to) == 3) { //Operate using currency code if (in_array($from, $CURRENCY)) { $in = $from; } if (in_array($to, $CURRENCY)) { $out = $to; } } else { echo "Error: You should either use 2 digit country codes or 3 digit currency codes!"; } if ($in && $out) { //Both currencies available, continue //Load live conversion rates and set as an array $XMLContent = file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"); if (is_array($XMLContent)) { foreach ($XMLContent as $line) { if (ereg("currency='([[:alpha:]]+)'", $line, $currencyCode)) { if (ereg("rate='([[:graph:]]+)'", $line, $rate)) { $RATES[$currencyCode[1]] = $rate[1]; } } } } if (is_array($RATES)) { $RATES["EUR"] = 1; //Add EUR reference to array if ($in != "EUR") { //Normalize rate to given input $BASE = $RATES[$in]; foreach ($RATES as $code => $rate) { $RATES[$code] = round($rate / $BASE, $sensitivity); } } //Prepend or append the currency information if ($sign) { if ($out == "USD") { echo "$"; } elseif ($out == "EUR") { echo "€"; } elseif ($out == "GBP") { echo "£"; } elseif ($out == "JPY") { echo "¥"; } else { $append = $out; } } echo round($amount * $RATES[$out], $sensitivity); //Output the converted amount if ($append) { echo " " . $append; } } else { echo "Error: Unable to load conversion rates!"; } } else { echo "Error: Either one or both of given currencies are not available for conversion!"; } } echo currency_convert("EUR", "USD", 1); ?> |
If possible, Can you please provide for all asian countries like dubai, malaysia, srilanka, pakisthan etc
Hello! Can somebody “update” this code? I mean comptatible with PHP 7.0. For example function “ereg” is already deprecated and it gives me some error!
Hi, I’d love to help out but I’m not doing any PHP for a long time. So, if any one willing to update the code, please let me know an I’ll put it up with your credit added. On a side note, have you tried using “preg_match” instead? There is some information here http://stackoverflow.com/questions/6270004/how-can-i-convert-ereg-expressions-to-preg-in-php
Hello,
Thank You for this nice script.This is awesome. You have made my day.
Regards,
Rumi
Still useful to this day :) Cheers Pixel Envision :)
Hi Guys,
I cannot getit to work. My PHP knowledge is not that good but please support.
I have one price shown with PHP code:
formatPrice( floatval($this->row->price) , JText::_(‘Consult us’) )
I need to show this price in USD, EUR, and TRY. Can anyone help me with this?
Appreciate!
Khan
Hi Khan,
Based on your post, I assume you can do something like that…
$converted=currency_convert(“CURRENCYOFTHEORIGINALAMOUNTHERE”,”USD”,$this->row->price,false);
formatPrice( floatval($converted) , JText::_(‘Consult us’) );
Hallo :)
I just wanted to thank you for your snippet. really useful indeed :)
Since I had to call your function several times on a webpage, I thought that maybe I could call only once the BCE xml. So I slightly changed your snippet as follows, and I hope it can be useful to you :) Thank you very much :)
s@ch@x
/* what I did is to take out $XMLContent and added a $ratesRequested. Both variables where latere referenced into your function with the “global” keyword. that’s it. this way the xml is requested only once. In my case was a speedup in performance. Once the site is online, I will add any credit you prefer to it. thank you ;) */
/* the code */
“USD”,”BE”=>”EUR”,”ES”=>”EUR”,”LU”=>”EUR”,”PT”=>”EUR”,”DE”=>”EUR”,”FR”=>”EUR”,”MT”=>”EUR”,”SI”=>”EUR”,”IE”=>”EUR”,”IT”=>”EUR”,”NL”=>”EUR”,”SK”=>”EUR”,”GR”=>”EUR”,”CY”=>”EUR”,”AT”=>”EUR”,”FI”=>”EUR”,”JP”=>”JPY”,”BG”=>”BGN”,”CZ”=>”CZK”,”DK”=>”DKK”,”EE”=>”EEK”,”GB”=>”GBP”,”HU”=>”HUF”,”LT”=>”LTL”,”LV”=>”LVL”,”PL”=>”PLN”,”RO”=>”RON”,”SE”=>”SEK”,”CH”=>”CHF”,”NO”=>”NOK”,”HR”=>”HRK”,”RU”=>”RUB”,”TR”=>”TRY”,”AU”=>”AUD”,”BR”=>”BRL”,”CA”=>”CAD”,”CN”=>”CNY”,”HK”=>”HKD”,”ID”=>”IDR”,”IN”=>”INR”,”KR”=>”KRW”,”MX”=>”MXN”,”MY”=>”MYR”,”NZ”=>”NZD”,”PH”=>”PHP”,”SG”=>”SGD”,”TH”=>”THB”,”ZA”=>”ZAR”);
if (strlen($from)==2 && strlen($to)==2) {//Operate using country code
if(isset($CURRENCY[$from])) {$in=$CURRENCY[$from];}
if(isset($CURRENCY[$to])) {$out=$CURRENCY[$to];}
} elseif (strlen($from)==3 && strlen($to)==3) {//Operate using currency code
if(in_array($from,$CURRENCY)) {$in=$from;}
if(in_array($to,$CURRENCY)) {$out=$to;}
} else {
echo “Error: You should either use 2 digit country codes or 3 digit currency codes!”;
}
if ($in && $out) {//Both currencies available, continue
if ( !$ratesRequested ) // we check if the XML was parsed, in case it is, we skip this step – s@ch@x
{
//Load live conversion rates and set as an array
$XMLContent= file(“http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml”);
if(is_array($XMLContent)) {
foreach ($XMLContent as $line) {
if (ereg(“currency='([[:alpha:]]+)'”,$line,$currencyCode)) {
if (ereg(“rate='([[:graph:]]+)'”,$line,$rate)) {
$RATES[$currencyCode[1]]=$rate[1];
}
}
}
}
$ratesRequested = true; // in case we are parsing the XML from BCE, we set this to true, so that we wont parse it again – s@ch@x
}
if(is_array($RATES))
{
$RATES[“EUR”]=1; //Add EUR reference to array
if ($in!=”EUR”) {//Normalize rate to given input
$BASE=$RATES[$in];
foreach ($RATES as $code => $rate)
{
$RATES[$code]=round($rate/$BASE,$sensitivity);
}
}
//Prepend or append the currency information
if ($sign)
{
if ($out==”USD”) {echo “$ “;}
elseif ($out==”EUR”) {echo “€ “;}
elseif ($out==”GBP”) {echo “£ “;}
elseif ($out==”JPY”) {echo “¥ “;}
else {$append=$out;}
}
echo “” . round($amount*$RATES[$out],$sensitivity) . ““;//Output the converted amount
if ($append) {echo ” “.$append;}
} else {
echo “Errore di caricamento: ricarica la pagina”;
}
} else {
echo “Error: Either one or both of given currencies are not available for conversion!”;
}
}
//echo currency_convert(“EUR”,”USD”,1);
?>
Hi s@ch@x,
You’re welcome, I’m just glad it’s ben useful for you! And thanks for sharing your updated code. :)