]*>/i', $table);
$lines = array();
foreach($trs as $i=>$l) {
$c = preg_split('/]*>/i', $l);
$lines[] = currency_clear_array($c);
}
foreach ($lines as $line) {
if ($line[0] && $line[1] && ($line[2] > 0)) {
$result[] = curency_prepare_rate(array(
'country' => 'BY',
'bank' => 'NBRB',
'fromvalue' => (int) $line[1],
'fromcurrency' => $line[0],
'tovalue' => doubleval(0.0 + $line[2]),
'tocurrency' => 'BYR',
'date' => $date,
));
}
}
return $result;
}
}
/**
* Parser
*
* Georgia (GE)
* National Bank of Georgia (NBG)
* Georgian lari (GEL)
* http://www.nbg.ge/
*
* @return
* Array of the currencies' exchange rates.
*/
function currency_nbg_get_rates() {
$url = 'http://www.nbg.ge/rss.php';
$result = array();
if (($datafetch = currency_http_request($url)) && ($html = $datafetch->data)) {
$tables = preg_split('/]*>/i', $html);
$table = isSet ($tables[1]) ? $tables[1] : "";
$trs = preg_split('/]*>/i', $table);
$lines = array();
foreach($trs as $i=>$l) {
$l = preg_replace('/<\/tr>.*/', '', $l);
$c = preg_split('/]*>/i', $l);
$lines[] = currency_clear_array($c);
}
$dtable = isSet ($tables[0]) ? $tables[0] : "";
$date = preg_match('/Currency Rates ([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])<\/title>/i', $dtable, $regs) ? $regs[1] . $regs[2] . $regs[3] : date('Ymd');
foreach ($lines as $line) {
if ($line[0] && $line[1] && $line[2]) {
$result[] = curency_prepare_rate(array(
'country' => 'GE',
'bank' => 'NBG',
'fromvalue' => (int) $line[1],
'fromcurrency' => $line[0],
'tovalue' => (double) (0.0 + $line[2]),
'tocurrency' => 'GEL',
'date' => $date,
));
}
}
}
return $result;
}
/**
* Parser
*
* Europe (EU)
* European Central Bank (ECB)
* Euro (EUR)
* http://www.ecb.europa.eu/
*
* @return
* Array of the currencies' exchange rates.
*/
function currency_ecb_get_rates() {
$url = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
$result = array();
if (($datafetch = currency_http_request($url)) && ($xml = simplexml_load_string($datafetch->data)) && isset($xml->Cube->Cube)) {
$cube = $xml->Cube->Cube;
$date = date('Ymd');
foreach($cube->attributes() as $n=>$v) if($n=='time') $date = implode('', explode('-', $v));
foreach ($cube->Cube as $cur) {
$fromvalue = $fromcurrency = NULL;
foreach($cur->attributes() as $n=>$v)
if($n=='currency') {
$fromcurrency = (string) $v;
}
elseif ($n=='rate') {
$fromvalue = $v;
}
if (!(is_null($fromvalue) || is_null($fromcurrency))) {
$result[] = curency_prepare_rate(array(
'country'=>'EU',
'bank'=>'ECB',
'fromvalue'=>1,
'fromcurrency'=>$fromcurrency,
'tovalue'=>(double) 1.0/(double) $fromvalue,
'tocurrency'=>'EUR',
'date'=>$date
));
}
}
}
return $result;
}
/**
* Parser
*
* Israil (IL)
* Bank of Israil (BOI)
* Israeli new sheqel (ILS)
* http://www.bankisrael.gov.il/
*
* @return
* Array of the currencies' exchange rates.
*/
function currency_boi_get_rates() {
$url = 'http://www.bankisrael.gov.il/currency.xml';
$result = array();
if (($datafetch = currency_http_request($url)) && ($xml = simplexml_load_string($datafetch->data)) && isset($xml->CURRENCY) && isset($xml->LAST_UPDATE) ) {
$date = implode('', explode('-', $xml->LAST_UPDATE));
foreach ($xml->CURRENCY as $cur) {
$fromvalue = isset($cur->UNIT) ? (int) $cur->UNIT : NULL;
$fromcurrency = isset($cur->CURRENCYCODE) ? (string) $cur->CURRENCYCODE : NULL;
$tovalue = isset($cur->RATE) ? (double) $cur->RATE : NULL;
if (!(is_null($fromvalue) || is_null($fromcurrency) || is_null($tovalue))) {
$result[] = curency_prepare_rate(array(
'country'=>'IL',
'bank'=>'BOI',
'fromvalue'=>$fromvalue,
'fromcurrency'=>$fromcurrency,
'tovalue'=>$tovalue,
'tocurrency'=>'ILS',
'date'=>$date
));
}
}
}
return $result;
}
/**
* Parser
*
* Kazakhstan (KZ)
* National Bank of Kazakhstan (NBK)
* Kazakhstani tenge (KZT)
* http://www.nationalbank.kz/
*
* @return
* Array of the currencies' exchange rates.
*/
function currency_nbk_get_rates() {
$url = 'http://www.nationalbank.kz/rss/rates_all.xml';
$result = array();
if (($datafetch = currency_http_request($url)) && ($xml = simplexml_load_string($datafetch->data)) && isset($xml->channel->item)) {
foreach ($xml->channel->item as $cur) {
$fromvalue = isset($cur->quant) ? (int) $cur->quant : NULL;
$fromcurrency = isset($cur->title) ? (string) $cur->title : NULL;
$tovalue = isset($cur->description) ? (double) $cur->description : NULL;
$date = '20'. implode('', array_reverse(explode('.', (string) $cur->pubDate)));
if (!(is_null($fromvalue) || is_null($fromcurrency) || is_null($tovalue) || empty($date))) {
$result[] = curency_prepare_rate(array(
'country'=>'KZ',
'bank'=>'NBK',
'fromvalue'=>$fromvalue,
'fromcurrency'=>$fromcurrency,
'tovalue'=>$tovalue,
'tocurrency'=>'KZT',
'date'=>$date
));
}
}
}
return $result;
}
/**
* Parser
*
* Canada (CA)
* Banque du Canada ()
* Canadian dollar (CAD)
* http://www.bankofcanada.ca/
*
* @return
* Array of the currencies' exchange rates.
*/
function currency_bdc_get_rates() {
$url = 'http://www.bankofcanada.ca/stats/assets/rates_rss/noon/en_all.xml';
$result = array();
if (($datafetch = currency_http_request($url)) && ($xml = simplexml_load_string($datafetch->data)) && isset($xml->item)) {
foreach ($xml->item as $cur) {
$tmp = $cur->children('cb', true);
$tmp = $tmp->statistics->exchangeRate;
if (isset($tmp->value, $tmp->baseCurrency, $tmp->targetCurrency, $tmp->observationPeriod)) {
$date = trim((string) $tmp->observationPeriod);
$result[] = curency_prepare_rate(array(
'country' => 'CA',
'bank' => 'BDC',
'fromvalue' => 1,
'fromcurrency' => (string) $tmp->targetCurrency,
'tovalue' => (double) 1.0/(double) $tmp->value,
'tocurrency' => (string) $tmp->baseCurrency,
'date' => substr($date, 0, 4) . substr($date, 5, 2) . substr($date, 8, 2),
));
}
}
}
return $result;
}
/**
* Parser
*
* China (CN)
* Bank of China (BOC)
* Chinese yuan (CNY)
* http://www.boc.cn/
*
* @return
* Array of the currencies' exchange rates.
*/
function currency_boc_get_rates() {
$url = 'http://www.boc.cn/sourcedb/whpj/enindex.html';
$result = array();
if (($datafetch = currency_http_request($url)) && ($html = $datafetch->data)) {
$tables = preg_split('/]*>/i', $html);
$table = isSet ($tables[8]) ? $tables[8] : "";
$trs = preg_split('/]*>/i', $table);
$lines = array();
foreach($trs as $i=>$l) {
$l = preg_replace('/<\/tr>.*/i', '', $l);
$c = preg_split('/]*>/i', $l);
$lines[] = currency_clear_array($c);
}
$date = date('Ymd');
foreach ($lines as $line) {
if ($line[0] && $line[1] && $line[3] && preg_match('/([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])/i', $line[6], $regs)) {
$date = $regs[1] . $regs[2] . $regs[3];
$result[] = curency_prepare_rate(array(
'country'=>'CN',
'bank'=>'BOC',
'fromvalue'=>100,
'fromcurrency'=>$line[0],
'tovalue'=>(doubleval ($line[1]) + doubleval ($line[3]))/2.0,
'tocurrency'=>'CNY',
'date'=>$date,
));
}
}
}
return $result;
}
/**
* Parser
*
* Mongolia (CN)
* Bank of Mongolia (BOM)
* Mongolian tugrik (MNT)
* http://www.mongolbank.mn/
*
* @return
* Array of the currencies' exchange rates.
*/
function currency_bom_get_rates() {
$url = 'http://www.mongolbank.mn/eng/dblistofficialdailyrate.aspx';
$result = array();
if (($datafetch = currency_http_request($url)) && ($html = $datafetch->data)) {
$tables = preg_split('/]*>/is', $html);
$trs = array_slice($tables, 12, 37);
$lines = array();
foreach($trs as $i=>$l) {
$l = preg_replace('/<\/span>.*/is', '', $l);
$c = spliti (']*>', $l);
$lines[] = currency_clear_array($c);
}
$date = date('Ymd');
$dtable = isSet ($tables[11]) ? $tables[11] : "";
$months = array('Jan'=>'01','Feb'=>'02','Mar'=>'03','Apr'=>'04','May'=>'05','Jun'=>'06','Jul'=>'07','Aug'=>'08','Sep'=>'09','Oct'=>'10','Nov'=>'11','Dec'=>'12');
if(preg_match('/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([0-9]{1,2}), ([0-9][0-9][0-9][0-9])/', $dtable, $regs)) {
$date = $regs[3].$months[$regs[1]].$regs[2];
}
foreach ($lines as $line) {
$line[2] = str_replace(array(' ', ','), '', $line[2]);
if ($line[1] && ($line[2] > 0)) {
$result[] = curency_prepare_rate(array(
'country'=>'MN',
'bank'=>'BOM',
'fromvalue'=>1,
'fromcurrency'=>$line[1],
'tovalue'=>doubleval($line[2]),
'tocurrency'=>'MNT',
'date'=>$date,
));
}
}
}
return $result;
}
/**
* Parser
*
* Russia (RU)
* Central Bank of Russia (CBR)
* Russian rouble (RUB)
* http://www.cbr.ru/
*
* @return
* Array of the currencies' exchange rates.
*/
function currency_cbr_get_rates() {
$url = 'http://www.cbr.ru/scripts/XML_daily.asp';
$result = array();
if (($datafetch = currency_http_request($url)) && ($xml = simplexml_load_string($datafetch->data)) && ($xml->getName()=='ValCurs')) {
$date = date('Ymd');
foreach($xml->attributes() as $n=>$v) if($n=='Date') $date = implode('', array_reverse(explode('.', $v)));
foreach ($xml->Valute as $cur)
if ($cur->getName()=='Valute' && isset($cur->CharCode, $cur->Nominal, $cur->Value))
$result[] = curency_prepare_rate(array(
'country' => 'RU',
'bank' => 'CBR',
'fromvalue' => str_replace (',', '.', (string) $cur->Nominal),
'fromcurrency' => (string) $cur->CharCode,
'tovalue' => str_replace(',', '.', (string) $cur->Value),
'tocurrency' => 'RUB',
'date' => $date
));
}
return $result;
}
/**
* Parser
*
* Ukraine (UA)
* National Bank of Ukraine (NBU)
* Ukrainian hryvnia (UAH)
* http://www.bank.gov.ua/
*
* @return
* Array of the currencies' exchange rates.
*/
function currency_nbu_get_rates() {
function currency_nbu_html_parse($html) {
$result = array();
$tables = preg_split('/]*>/i', $html);
$table = isSet ($tables[6]) ? $tables[6] : "";
$trs = preg_split('/]*>/i', $table);
unset($trs[0], $trs[1]);
$lines = array();
foreach($trs as $i=>$l) {
$l = preg_replace('/<\/tr>.*/i', '', $l);
$c = preg_split('/]*>/i', $l);
$lines[] = currency_clear_array($c);
}
$dtable = isSet ($tables[5]) ? $tables[5] : "";
$date = preg_match('/([0-9][0-9]).([0-9][0-9]).([0-9][0-9][0-9][0-9])/i', $dtable, $regs) ? $regs[3] . $regs[2] . $regs[1] : date('Ymd');
foreach ($lines as $line) {
if ($line[1] && $line[2] && $line[4]) {
$result[] = curency_prepare_rate(array(
'country'=>'UA',
'bank'=>'NBU',
'fromvalue'=>$line[2],
'fromcurrency'=>$line[1],
'tovalue'=>doubleval($line[4]),
'tocurrency'=>'UAH',
'date'=>$date,
));
}
}
return $result;
}
$d_url = 'http://bank.gov.ua/control/en/curmetal/detail/currency?period=daily';
$m_url = 'http://bank.gov.ua/control/en/curmetal/detail/currency?period=monthly';
$result = array();
if (($datafetch = currency_http_request($d_url)) && ($html = $datafetch->data)) {
$result = currency_nbu_html_parse($html);
}
if (($datafetch = currency_http_request($m_url)) && ($html = $datafetch->data)) {
$result = array_merge($result, currency_nbu_html_parse($html));
}
return $result;
}
/**
* Common functions for the project.
*
*/
/**
* Prepare rates to readable view
*
* Prepare currencies' exchange rates to readable view.
*
* @param $rate
* Array with a currency's rate.
* @param $low_limit
* The lower limit of the normalization.
* @param $precision
* Rounding precision of the rate.
* @return
* Normalizing and rounding rate.
*/
function curency_prepare_rate($rate, $low_limit = 20, $precision = 2) {
$low_limit = (int) $low_limit;
$high_limit = $low_limit*10;
$precision = (int) $precision;
$tovalue = $rate['tovalue'];
$fromvalue = $rate['fromvalue'];
if($tovalue < $low_limit) {
while ($tovalue < $low_limit) {
$fromvalue = $fromvalue*10;
$tovalue = $tovalue*10.;
}
}
else if ($tovalue>=$high_limit) {
while ($tovalue>=$high_limit) {
$fromvalue = $fromvalue/10;
$tovalue = $tovalue/10.;
}
}
$rate['tovalue'] = round($tovalue, $precision);
$rate['fromvalue'] = $fromvalue;
return $rate;
}
/**
* Clear array of the strings from HTML-code
*
* @param $arr
* The array of the strings to be cleared.
* @return
* Cleared array of the strings.
*/
function currency_clear_array($arr) {
$res = array();
$s = array(' | ', ' ', ' | | | |