PHP實(shí)現(xiàn)貨幣換算的方法
本文實(shí)例講述了PHP實(shí)現(xiàn)貨幣換算的方法。分享給大家供大家參考。
具體實(shí)現(xiàn)代碼如下:
/*
* File: CurrencyConverter.php
* Author: Simon Jarvis
* Copyright: 2005 Simon Jarvis
* Date: 10/12/05
* Link: http://www.white-hat-web-design.co.uk/articles/php-currency-conversion.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/
class CurrencyConverter {
var $xml_file = "www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
var $mysql_host, $mysql_user, $mysql_pass, $mysql_db, $mysql_table;
var $exchange_rates = array();
//Load Currency Rates
function CurrencyConverter($host,$user,$pass,$db,$tb) {
$this->mysql_host = $host;
$this->mysql_user = $user;
$this->mysql_pass = $pass;
$this->mysql_db = $db;
$this->mysql_table = $tb;
$this->checkLastUpdated();
$conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass);
$rs = mysql_select_db($this->mysql_db,$conn);
$sql = "SELECT * FROM ".$this->mysql_table;
$rs = mysql_query($sql,$conn);
while($row = mysql_fetch_array($rs)) {
$this->exchange_rates[$row['currency']] = $row['rate'];
}
}
/* Perform the actual conversion, defaults to £1.00 GBP to USD */
function convert($amount=1,$from="GBP",$to="USD",$decimals=2) {
return(number_format(($amount/$this->exchange_rates[$from])*$this->exchange_rates[$to],$decimals));
}
/* Check to see how long since the data was last updated */
function checkLastUpdated() {
$conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass);
$rs = mysql_select_db($this->mysql_db,$conn);
$sql = "SHOW TABLE STATUS FROM ".$this->mysql_db." LIKE '".$this->mysql_table."'";
$rs = mysql_query($sql,$conn);
if(mysql_num_rows($rs) == 0 ) {
$this->createTable();
} else {
$row = mysql_fetch_array($rs);
if(time() > (strtotime($row["Update_time"])+(12*60*60)) ) {
$this->downloadExchangeRates();
}
}
}
/* Download xml file, extract exchange rates and store values in database */
function downloadExchangeRates() {
$currency_domain = substr($this->xml_file,0,strpos($this->xml_file,"/"));
$currency_file = substr($this->xml_file,strpos($this->xml_file,"/"));
$fp = @fsockopen($currency_domain, 80, $errno, $errstr, 10);
if($fp) {
$out = "GET ".$currency_file." HTTP/1.1rn";
$out .= "Host: ".$currency_domain."rn";
$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5rn";
$out .= "Connection: Closernrn";
fwrite($fp, $out);
while (!feof($fp)) {
$buffer .= fgets($fp, 128);
}
fclose($fp);
$pattern = "{<Cubes*currency='(w*)'s*rate='([d.]*)'/>}is";
preg_match_all($pattern,$buffer,$xml_rates);
array_shift($xml_rates);
for($i=0;$i<count($xml_rates[0]);$i++) {
$exchange_rate[$xml_rates[0][$i]] = $xml_rates[1][$i];
}
$conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass);
$rs = mysql_select_db($this->mysql_db,$conn);
foreach($exchange_rate as $currency=>$rate) {
if((is_numeric($rate)) && ($rate != 0)) {
$sql = "SELECT * FROM ".$this->mysql_table." WHERE currency='".$currency."'";
$rs = mysql_query($sql,$conn) or die(mysql_error());
if(mysql_num_rows($rs) > 0) {
$sql = "UPDATE ".$this->mysql_table." SET rate=".$rate." WHERE currency='".$currency."'";
} else {
$sql = "INSERT INTO ".$this->mysql_table." VALUES('".$currency."',".$rate.")";
}
$rs = mysql_query($sql,$conn) or die(mysql_error());
}
}
}
}
/* Create the currency exchange table */
function createTable() {
$conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass);
$rs = mysql_select_db($this->mysql_db,$conn);
$sql = "CREATE TABLE ".$this->mysql_table." ( currency char(3) NOT NULL default '', rate float NOT NULL default '0', PRIMARY KEY(currency) ) ENGINE=MyISAM";
$rs = mysql_query($sql,$conn) or die(mysql_error());
$sql = "INSERT INTO ".$this->mysql_table." VALUES('EUR',1)";
$rs = mysql_query($sql,$conn) or die(mysql_error());
$this->downloadExchangeRates();
}
}
?>
上面的代碼復(fù)制到一個(gè)新文件并將其保存為CurrencyConverter.php。當(dāng)你需要轉(zhuǎn)換包含類文件,稱為“轉(zhuǎn)換”功能。你需要輸入自己的mysql數(shù)據(jù)庫(kù)變量如登錄詳細(xì)信息。下面的例子將£2.50英鎊轉(zhuǎn)換成美元(美元)。
include('CurrencyConverter.php');
$x = new CurrencyConverter('your_host','your_username','your_password','your_database_name','your_table_name');
echo $x->convert(2.50,'GBP','USD');
?>
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- C# .net實(shí)現(xiàn)貨幣轉(zhuǎn)換示例
- Java把數(shù)字格式化為貨幣字符串實(shí)例代碼
- asp.net 獲取銀行貨幣匯率的代碼
- SQL貨幣數(shù)字轉(zhuǎn)英文字符語(yǔ)句
- 用javascript判斷輸入數(shù)據(jù)是否貨幣并自動(dòng)添加¥符號(hào)的代碼
- javascript實(shí)現(xiàn)的平方米、畝、公頃單位換算小程序
- 進(jìn)制轉(zhuǎn)換算法原理(二進(jìn)制 八進(jìn)制 十進(jìn)制 十六進(jìn)制)
- php實(shí)現(xiàn)的樹形結(jié)構(gòu)數(shù)據(jù)存取類實(shí)例
- PHP使用get_headers函數(shù)判斷遠(yuǎn)程文件是否存在的方法
- php的mssql數(shù)據(jù)庫(kù)連接類實(shí)例
相關(guān)文章
學(xué)習(xí)thinkphp5.0驗(yàn)證類使用方法
這篇文章主要介紹了thinkphp5.0驗(yàn)證類的簡(jiǎn)單有效的使用方法,一起學(xué)習(xí)下。2017-11-11php-redis中的sort排序函數(shù)總結(jié)
這篇文章主要介紹了php-redis中的sort排序函數(shù)總結(jié),本文講解了了按字母排序、排序取部分?jǐn)?shù)據(jù)、使用外部key進(jìn)行排序等排序方法,同時(shí)給出代碼實(shí)例,需要的朋友可以參考下2015-07-07PHP處理數(shù)組和XML之間的互相轉(zhuǎn)換
這篇文章主要介紹了如何使用PHP處理數(shù)組和XML之間的互相轉(zhuǎn)換,詳細(xì)介紹了PHP將XML轉(zhuǎn)換成數(shù)組,PHP將數(shù)組轉(zhuǎn)換成XML的方法,感興趣的小伙伴們可以參考一下2016-06-06利用PHP計(jì)算有多少小于當(dāng)前數(shù)字的數(shù)字方法示例
這篇文章主要給大家介紹了關(guān)于利用PHP計(jì)算有多少小于當(dāng)前數(shù)字的數(shù)字的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08php 數(shù)組的創(chuàng)建、調(diào)用和更新實(shí)現(xiàn)代碼
對(duì)于php的數(shù)組是php中很重要的一個(gè)地方,大家一定要仔細(xì)看。2009-03-03php中g(shù)etservbyport與getservbyname函數(shù)用法實(shí)例
這篇文章主要介紹了php中g(shù)etservbyport與getservbyname函數(shù)用法,以實(shí)例形式分析了getservbyport與getservbyname函數(shù)獲取server端的端口等信息的方法,需要的朋友可以參考下2014-11-11PHP編程實(shí)現(xiàn)陽(yáng)歷轉(zhuǎn)換為陰歷的方法實(shí)例
這篇文章主要介紹了PHP編程實(shí)現(xiàn)陽(yáng)歷轉(zhuǎn)換為陰歷的方法,結(jié)合具體實(shí)例形式分析了php陰歷操作類的定義與使用技巧,需要的朋友可以參考下2017-08-08