Answer the question
In order to leave comments, you need to log in
How to make automatic currency conversion against the ruble?
Hello!
There is a need to set up automatic price conversion from rubles to some others.
There are a number of blocks:
<span class="rub">1000</span> рублей
<span class="usd">????</span> долларов
<span class="eur">????</span> евро
<span class="yah">????</span> гривен
<span class="byr">????</span> белорусских рублей
It is desirable that the conversion takes place relative to the current exchange rate of the Central Bank. Answer the question
In order to leave comments, you need to log in
Many thanks to @EnterSandman and @Taraflex for the hints. I solved the problem in the following way:
Create a PHP exchange rate parser file:
<?php
$content = get_content();
$pattern = "#<Valute ID=\"([^\"]+)[^>]+>[^>]+>([^<]+)[^>]+>[^>]+>[^>]+>[^>]+>[^>]+>[^>]+>([^<]+)[^>]+>[^>]+>([^<]+)#i";
preg_match_all($pattern, $content, $out, PREG_SET_ORDER);
$usd = "";
$eur = "";
$byr = "";
$uah = "";
$kzt = "";
foreach($out as $cur)
{
if($cur[2] == 840) $usd = str_replace(",",".",$cur[4]);
if($cur[2] == 978) $eur = str_replace(",",".",$cur[4]);
if($cur[2] == 974) $byr = str_replace(",",".",$cur[4]);
if($cur[2] == 980) $uah = str_replace(",",".",$cur[4]);
if($cur[2] == 398) $kzt = str_replace(",",".",$cur[4]);
}
echo "<span id=\"bank-usd\">".$usd."</span> <span id=\"bank-eur\">".$eur."</span> <span id=\"bank-byr\">".$byr."</span> <span id=\"bank-uah\">".$uah."</span> <span id=\"bank-kzt\">".$kzt."</span>";
function get_content()
{
$link = "http://www.cbr.ru/scripts/XML_daily.asp";
$fd = fopen($link, "r");
$text="";
echo "";
while (!feof ($fd)) $text .= fgets($fd, 4096);
fclose ($fd);
return $text;
}
?>
Anywhere in the HTML document, we insert the download of this file, and wrap all its contents in display: none. var rub = 1000;
var bankUSD = document.getElementById('bank-usd').innerText;
var bankEUR = document.getElementById('bank-eur').innerText;
var bankBYR = document.getElementById('bank-byr').innerText;
var bankUAH = document.getElementById('bank-uah').innerText;
var bankKZT = document.getElementById('bank-kzt').innerText;
document.getElementById('rub').innerHTML = rub;
document.getElementById('usd').innerHTML = (rub/bankUSD).toFixed(2);
document.getElementById('eur').innerHTML = (rub/bankEUR).toFixed(2);
document.getElementById('byr').innerHTML = (10000*rub/bankBYR).toFixed(0);
document.getElementById('uah').innerHTML = (10*rub/bankUAH).toFixed(1);
document.getElementById('kzt').innerHTML = (100*rub/bankKZT).toFixed(0);
For the Belarusian, Kazakh and Ukrainian currencies, I had to add a few more zeros for the correct output. <span id="rub"></span> рублей
<span id="byr"></span> рублей
<span id="uah"></span> гривен
<span id="kzt"></span> тенге
<span id="usd"></span> долларов
<span id="eur"></span> евро
I'm not strong in JS, but I'll give you direction
jsfiddle.net/Bn7X9
The exchange rate can be found here
www.cbr.ru/scripts/XML_daily.asp
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question