Skip to content

Instantly share code, notes, and snippets.

@comxd
Forked from dcblogdev/gist:8067095
Last active January 24, 2018 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save comxd/cbb4e4630ce76159367c168555ec2900 to your computer and use it in GitHub Desktop.
Save comxd/cbb4e4630ce76159367c168555ec2900 to your computer and use it in GitHub Desktop.
Use Google finance calculator to convert currency with PHP 7+
<?php
function convertCurrency(float $amount, string $from, string $to){
$data = file_get_contents("https://finance.google.com/finance/converter?a={$amount}&from={$from}&to={$to}");
preg_match("/<span class=bld>([0-9\.]+) " . $currency_to . "<\/span>/", $data, $matches);
return (float)$matches[1];
}
echo convertCurrency(10.00, "GBP", "USD");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment