热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PHP实现货币换算的方法

这篇文章主要介绍了PHP实现货币换算的方法,以实例形式较为详细的讲述了货币转换的实现方法,并举英镑转换美元为例讲述具体用法,需要的朋友可以参考下
本文实例讲述了PHP实现货币换算的方法。分享给大家供大家参考。

具体实现代码如下:

代码如下:

<&#63;php
/*
* 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 = "{}is";
preg_match_all($pattern,$buffer,$xml_rates);
array_shift($xml_rates);
for($i=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();
}
}
&#63;>


上面的代码复制到一个新文件并将其保存为CurrencyConverter.php。当你需要转换包含类文件,称为“转换”功能。你需要输入自己的mysql数据库变量如登录详细信息。下面的例子将£2.50英镑转换成美元(美元)。

代码如下:

<&#63;php
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');
&#63;>

希望本文所述对大家的php程序设计有所帮助。

推荐阅读
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了使用cacti监控mssql 2005运行资源情况的操作步骤,包括安装必要的工具和驱动,测试mssql的连接,配置监控脚本等。通过php连接mssql来获取SQL 2005性能计算器的值,实现对mssql的监控。详细的操作步骤和代码请参考附件。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • Redis底层数据结构之压缩列表的介绍及实现原理
    本文介绍了Redis底层数据结构之压缩列表的概念、实现原理以及使用场景。压缩列表是Redis为了节约内存而开发的一种顺序数据结构,由特殊编码的连续内存块组成。文章详细解释了压缩列表的构成和各个属性的含义,以及如何通过指针来计算表尾节点的地址。压缩列表适用于列表键和哈希键中只包含少量小整数值和短字符串的情况。通过使用压缩列表,可以有效减少内存占用,提升Redis的性能。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • Windows下配置PHP5.6的方法及注意事项
    本文介绍了在Windows系统下配置PHP5.6的步骤及注意事项,包括下载PHP5.6、解压并配置IIS、添加模块映射、测试等。同时提供了一些常见问题的解决方法,如下载缺失的msvcr110.dll文件等。通过本文的指导,读者可以轻松地在Windows系统下配置PHP5.6,并解决一些常见的配置问题。 ... [详细]
  • Metasploit攻击渗透实践
    本文介绍了Metasploit攻击渗透实践的内容和要求,包括主动攻击、针对浏览器和客户端的攻击,以及成功应用辅助模块的实践过程。其中涉及使用Hydra在不知道密码的情况下攻击metsploit2靶机获取密码,以及攻击浏览器中的tomcat服务的具体步骤。同时还讲解了爆破密码的方法和设置攻击目标主机的相关参数。 ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • yum安装_Redis —yum安装全过程
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Redis—yum安装全过程相关的知识,希望对你有一定的参考价值。访问https://redi ... [详细]
  • 本文介绍了关系型数据库和NoSQL数据库的概念和特点,列举了主流的关系型数据库和NoSQL数据库,同时描述了它们在新闻、电商抢购信息和微博热点信息等场景中的应用。此外,还提供了MySQL配置文件的相关内容。 ... [详细]
author-avatar
蜡笔小昕廖廖
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有