利用PHP怎么对货币进行换算

介绍

利用PHP怎么对货币进行换算吗?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

具体实现代码如下:

代码如下:

& lt; ?php
/*
*文件:CurrencyConverter。php
*作者:西蒙·贾维斯
*版权:2005西蒙·贾维斯
*日期:10/12/05
*链接:http://www.white-hat-web-design.co.uk/articles/php-currency-conversion.php
*
*这个程序是自由软件;你可以重新分配和/或
*修改它根据GNU通用公共许可证
*由自由软件基金会发布;版本2
*的许可,或(任您选)其后的版本。
*
*这个程序是分布式的,希望这将是有用的,
*但没有任何保修;甚至没有
*的默示保证适销性或健身为特定目的。看到
* GNU通用公共许可证的更多细节:http://www.gnu.org/licenses/gpl.html

* * *
/
类CurrencyConverter {
,,var xml_file=懊涝?www.ecb.int/统计/eurofxref/eurofxref-daily.xml";
,,var mysql_host美元,美元mysql_user mysql_pass美元,mysql_db美元,美元mysql_table;
,,var数组$ exchange_rates=();
,,//加载汇率
,,函数CurrencyConverter(主机、用户、美元,美元$ db,结核病美元){
,,,,,$ this→mysql_host=$主机;
,,,,,$ this→mysql_user=$用户;
,,,,,$ this→mysql_pass=美元通过;
,,,,,$ this→mysql_db=$ db;
,,,,,$ this→mysql_table=$结核病;
,,,,,$ this→checkLastUpdated ();
,,,,,康涅狄格州美元=mysql_connect ($ this→mysql_host, $ this→mysql_user, $ this→mysql_pass);
,,,,,美元rs=mysql_select_db ($ this→mysql_db,康涅狄格州美元);
,,,,,从“$ sql=癝ELECT *。$ this→mysql_table;
,,,,,$ rs=,mysql_query (sql,康涅狄格州美元);
,,,,,虽然美元($ result中进行row=mysql_fetch_array rs)) {
,,,,,,,,$ this→exchange_rates[$行[& # 39;货币# 39;]]=$ row[& # 39;率# 39;];
,,,,,}
,,}
,,/*执行实际的转换,默认为,英镑;1.00英镑美元*/
,,函数转换(数量=1美元=癎BP",美元=癠SD",小数=2美元){
,,,,,返回(number_format((数量/美元这个→exchange_rates[美元])* $ this→exchange_rates[美元],小数美元));
,,}
,,/*查看数据以来持续多久更新*/
,,函数checkLastUpdated () {
,,,,,康涅狄格州美元=mysql_connect ($ this→mysql_host, $ this→mysql_user, $ this→mysql_pass);
,,,,,美元rs=mysql_select_db ($ this→mysql_db,康涅狄格州美元);
,,,,,$ sql=跋允颈碜刺印啊? this→mysql_db !”像& # 39;“。$ this→mysql_table !”& # 39;“;
,,,,,$ rs=,mysql_query (sql,康涅狄格州美元);
,,,,,如果(mysql_num_rows (rs)==0) {
,,,,,,,,$ this→不知道();
,,,,,其他}{
,,,,,,,,美元($ result中进行row=mysql_fetch_array rs);
,,,,,,,,如果时间()比;(strtotime(美元行[“Update_time"]) + (12 * 60 * 60))) {
,,,,,,,,,,,$ this→downloadExchangeRates ();
,,,,,,,,}
,,,,,}
,,}
,,/*下载xml文件,提取汇率和值存储在数据库*/
,,函数downloadExchangeRates () {
,,,,,$ currency_domain=substr ($ this→xml_file, 0,大小写敏感($ this→xml_file,“/?);
,,,,,$ currency_file=substr ($ this→xml_file,大小写敏感($ this→xml_file,“/?);
,,,,,(fp=@fsockopen美元currency_domain 80美元errno, errstr美元,10);
,,,,,如果美元(fp) {
,,,,,,,,美元=?得到“。美元currency_file !”HTTP/1.1 rn";
,,,,,,,,美元。=爸鞒秩?“。currency_domain美元霸主地位;rn";
,,,,,,,,美元。=坝没Т?Mozilla/5.0(窗口;U;Windows NT 5.1;en - us;房车:Firefox 1.8)壁虎/20051111/1.5 rn";
,,,,,,,,美元。=傲?Closernrn";null

利用PHP怎么对货币进行换算