RoxRates.php
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
<?php
/**
* Project:     ROX Rates
* File:        RoxRates.php
* Author:      Mart?Clasen <martin.clasen AT gmail DOT com>
* Website:     http://www.clasen.com.ar
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* Author:      Mart?Clasen <martin.clasen AT gmail DOT com>
* @version 1.5

    Argentine Peso = ARS     Australian Dollar = AUD      Bahrain Dinar = BHD         Botswana Pula = BWP       Brazilian Real = BRL
    Brunei Dollar = BND      Canadian Dollar = CAD        Chilean Peso = CLP          Chinese Yuan = CNY        Colombian Peso = COP
    Cyprus Pound = CYP       Czech Koruna = CZK           Danish Krone = DKK          Euro = EUR                Hungarian Forint = HUF
    Icelandic Krona = ISK    Indian Rupee = INR           Ind Rupiah = IDR            Iranian Rial = IRR        Israeli New Sheqel = ILS
    Japanese Yen = JPY       Korean Won = KRW             Kuwaiti Dinar = KWD         Libyan Dinar = LYD        Malaysian Ringgit = MYR
    Maltese Lira = MTL       Mauritian Rupee = MUR        Mexican Peso = MXN          Nepalese Rupee = NPR      New Zealand Dollar = NZD
    Norwegian Krone = NOK    Omani Rial = OMR             Pakistan Rupee = PKR        Polish Zloty = PLN        Pound Sterling = GBP
    Qatar Riyal = QAR        Saudi Arabian Riyal = SAR    Singapore Dollar = SGD      Slovenian Tolar = SIT     South African Rand = ZAR
    Sri Lanka Rupee = LKR    Swedish Krona = SEK          Swiss Franc = CHF           Thai Baht = THB           Trinidad & Tobago Dollar = TTD
    U.A.E. Dirham = AED      U.S. Dollar = USD            Venezuelan Bolivar = VEB

*/

/**
* return
* @param    $from (string) string value of currency code source
* @param    $to (string) string value of currency code target
* @param    $round (integer) number of digits after the decimal point. Optional.
*
* @return   float rate of conversion
*/
function rate($from,$to,$round=false)
{
    static
$rate;
    
$from = strtoupper($from);
    
$to = strtoupper($to);

    if(!isset(
$rate))
    {
        
//http://www.currencysource.com/rss_currencyexchangerates.html
        
$xml = file_get_contents('http://currencysource.com/RSS/USD.xml');
        
preg_match_all('/([A-Z]{3}) \((.*)\)/',$xml,$xml_match);
        
$rate = array_combine($xml_match[1], $xml_match[2]);
    }

    if(!isset(
$rate[$to], $rate[$from]))
        return
false;

    
$out = $rate[$to] / $rate[$from];
    if(
is_numeric($round))
        
$out = round($out, $round);
    return
$out;
}

//EXAMPLE USAGE
echo '<pre>';
echo
'Con 50 USD me puedo comprar '.(50*rate('usd','eur',3)).' EUR o '.(50*rate('usd','ars',4)).' ARS';
echo
"\n";
echo
'Y con 50 YEN me compro '.(50*rate('jpy','usd',6)).' USD o '.(50*rate('jpy','eur')).' EUR';
echo
"\nUSD = $ United States Dollar\nEUR = € Euros\nARS = $ Pesos Argentinos\nJPY = ¥ Japanese Yen"

/* --- Example Output ---
    Con 50 USD me puedo comprar 37.05 EUR o 150.88 ARS
    Y con 50 YEN me compro 0.41085 USD o 0.304555838765 EUR
    USD = $ United States Dollar
    EUR = € Euros
    ARS = $ Pesos Argentinos
    JPY = ¥ Japanese Yen
*/

?>
PHP-Hispano.net - Porque al final, todos acabamos aprendiendo.