Hmm... yes... Well, let me see if I can make a dynamic one.....
~Brett
EDIT
Updated the script. Now creates a text file in this form:
[USD]:USD_1.00000_1.00000000,EUR_0.823280_1.21465358,GBP_0.564715_1.77080474,JPY_114.465_0.00873629,CAD_1.16430_0.85888516,AUD_1.32670_0.75374990,CHF_1.27020_0.78727759,RUB_28.6700_0.03487966,CNY_8.06754_0.12395352,ZAR_6.14499_0.16273419
[code=php]<?php
function get_rates($cntry)
{
$countries = array('USD', 'EUR', 'GBP');
if(!in_array($cntry, $countries))
{
return FALSE;
}
$invs = array();
$info = array();
$to_file = '';
$contents = file_get_contents('http://www.xe.com');
$txt_cntrs = substr($contents, strpos($contents, '<tr valign="middle" align="center" bgcolor=#FFFFFF class="t_dat">'), strpos($contents, '</tr>', strpos($contents, '<tr valign="middle" align="center" bgcolor=#FFFFFF class="t_dat">'))-strpos($contents, '<tr valign="middle" align="center" bgcolor=#FFFFFF class="t_dat">'));
$txt_rates = substr($contents, strpos($contents, '<td> <b>1 '.$cntry.' =</b></td>'), strpos($contents, '</tr>', strpos($contents, '</table>', strpos($contents, '<td> <b>1 '.$cntry.' =</b></td>')))-strpos($contents, '<td> <b>1 '.$cntry.' =</b></td>'));
$pat_rates = "@<td> <b>1 ".$cntry." =<\/b><\/td>[\n\s]*<\/tr><\/table><\/td>[\n\s]*<td> ([\d\.]*) <\/td>[\n\s]*<td> ([\d\.]*) <\/td>[\n\s]*<td> ([\d\.]*) <\/td>[\n\s]*<td> ([\d\.]*) <\/td>[\n\s]*<td> ([\d\.]*) <\/td>[\n\s]*<td> ([\d\.]*) <\/td>[\n\s]*<td> ([\d\.]*) <\/td>[\n\s]*<td> ([\d\.]*) <\/td>[\n\s]*<td> ([\d\.]*) <\/td>[\n\s]*<td> ([\d\.]*) <\/td>[\n\s]*<td> ([\d\.]*) <\/td>[\n\s]*@m";
$pat_cntrs = "@<tr valign=\"[\w]*\" align=\"[\w]*\" bgcolor=#[a-zA-Z0-9]* class=\"[\w]*\">[\n\s]*<td bgcolor=#[a-zA-Z0-9]*><a href=\"[\w\.\/\:]*\"><IMG SRC=\"[\w\.\/\-\:]*\" height=[\d]* width=[\d]* alt=\"[\w\.]*\" align=\"[\w]*\" border=[\d]*></a></td>[\n\s]*<td><IMG SRC=\"[\w\:\/\.\-]*\" ALT=\"[\w]*\" HEIGHT=[\d]* width=[\d]*><br>([\w]*)</td>[\n\s]*<td><IMG SRC=\"[\w\:\/\.\-]*\" ALT=\"[\w]*\" HEIGHT=[\d]* width=[\d]*><br>([\w]*)</td>[\n\s]*<td><IMG SRC=\"[\w\:\/\.\-]*\" ALT=\"[\w]*\" HEIGHT=[\d]* width=[\d]*><br>([\w]*)</td>[\n\s]*<td><IMG SRC=\"[\w\:\/\.\-]*\" ALT=\"[\w]*\" HEIGHT=[\d]* width=[\d]*><br>([\w]*)</td>[\n\s]*<td><IMG SRC=\"[\w\:\/\.\-]*\" ALT=\"[\w]*\" HEIGHT=[\d]* width=[\d]*><br>([\w]*)</td>[\n\s]*<td><IMG SRC=\"[\w\:\/\.\-]*\" ALT=\"[\w]*\" HEIGHT=[\d]* width=[\d]*><br>([\w]*)</td>[\n\s]*<td><IMG SRC=\"[\w\:\/\.\-]*\" ALT=\"[\w]*\" HEIGHT=[\d]* width=[\d]*><br>([\w]*)</td>[\n\s]*<td><IMG SRC=\"[\w\:\/\.\-]*\" ALT=\"[\w]*\" HEIGHT=[\d]* width=[\d]*><br>([\w]*)</td>[\n\s]*<td><IMG SRC=\"[\w\:\/\.\-]*\" ALT=\"[\w]*\" HEIGHT=[\d]* width=[\d]*><br>([\w]*)</td>[\n\s]*<td><IMG SRC=\"[\w\:\/\.\-]*\" ALT=\"[\w]*\" HEIGHT=[\d]* width=[\d]*><br>([\w]*)</td>[\n\s]*<td><IMG SRC=\"[\w\:\/\.\-]*\" ALT=\"[\w]*\" HEIGHT=[\d]* width=[\d]*><br>([\w]*)</td>[\n\s]*@m";
preg_match($pat_rates, $txt_rates, $rates);
preg_match($pat_cntrs, $txt_cntrs, $cntrs);
array_shift($rates);
array_shift($cntrs);
$base = array_search('1.00000', $rates);
foreach($rates as $rate)
{
$inverse = round($rates[$base]/$rate, 9);
if(!strpos($inverse, '.'))
{
$inverse .= '.00000000';
}
else
{
$inv = substr($inverse, strpos($inverse, '.')+1);
$chars = strlen($inv);
if($chars<8)
{
for($n=1; $n<=(8-$chars); $n++)
{
$inverse .= '0';
}
}
else if($chars>8)
{
$over = $chars-8;
$inverse = substr($inverse, 0, ($over*-1));
}
else
{
$inverse = $inverse;
}
}
$invs[] = $inverse;
}
for($i=0; $i<(count($rates)-1); $i++)
{
$info[$cntrs[$i]] = $rates[$i].'_'.$invs[$i];
}
foreach($info as $country => $rates)
{
$to_file .= $country."_".$rates.",";
}
$to_file = substr($to_file, 0, -1);
return $to_file;
}
$filename = 'xrate.txt';
$usd = get_rates('USD');
$eur = get_rates('EUR');
$gbp = get_rates('GBP');
$to_file = "[USD]:".$usd."\n[EUR]:".$eur."\n[GBP]:".$gbp;
$fp = fopen($filename, "w");
if((fwrite($fp, $to_file))===FALSE)
{
$message = "There was an error updating the exchange rate text file. Double check the settings.\n\n
----------------------------------------------------------
Attempted at ".date('H:i:s')." on ".date('m.d.Y');
mail('patterson.web.services@gmail.com', '[PWS] ERROR: Currency Update', $message, "FROM: PWS Errors <empty@patternet.com>");
}
chmod($filename, 0777);
fclose($fp);
?>
Just a side-note, xe.com does offer a feed (or three) to get this info into an XML or CSV or HTML output. It costs a pretty penny, but they do have a "feed" service. MIght be worth looking at if you're going to be making numerous calls per day. So, this will put all 3 countries into my txt file, and update every 6 hours. Hope it helps.
For each country you need (currently only supports 3: "USD", "EUR", "GBP"), just run the function.
~Brett