Skip to content

Instantly share code, notes, and snippets.

@M-O-Z-G
Last active June 19, 2016 12:56
Show Gist options
  • Save M-O-Z-G/c945d5ec8b4f9ac4bc34abc1b5a08cbe to your computer and use it in GitHub Desktop.
Save M-O-Z-G/c945d5ec8b4f9ac4bc34abc1b5a08cbe to your computer and use it in GitHub Desktop.
PHP currency converter with Google
<?php
function converterCurrency($price) {
$price = (float) $price;
if ( $country == 'ru') { //Your switching rule here
$cur_glyph = ' &#8381;'; // Currency Glyph
$amount = urlencode($price);
$from_Currency = urlencode('USD'); //Initial Currency
$to_Currency = urlencode('RUB'); //Target Currency
$get = file_get_contents("https://www.google.com/finance/converter?a=$amount&from=$from_Currency&to=$to_Currency");
$get = explode("<span class=bld>", $get);
$get = explode("</span>", $get[1]);
$converted_amount = round((string) preg_replace("/[^0-9\.]/", null, $get[0]), 2);
$result = $converted_amount . $cur_glyph; //Output
} else {
$cur_glyph = '$';
$result = $cur_glyph . $price; //Default Output
}
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment