Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

Use Google finance calculator to convert currency with PHP

David Carr

Tutorials PHP & MySQL

Convert currency from one country to another easily with Google's finance calculator, using that it's possible to collect the output using PHP.</p>

Altaf Hussain wrote an excellent function to collect currencies from Google finance you pass the amount, currency from and currency to be converted to.

This example below compresses the code slightly also wraps the final output inside a number_format function to keep the currency in whole number format.

function convertCurrency($amount, $from, $to){
    $data = file_get_contents("$amount&from=$from&to=$to");
    preg_match("/<span class=bld>(.*)</span>/",$data, $converted);
    $converted = preg_replace("/[^0-9.]/", "", $converted[1]);
    return number_format(round($converted, 3),2);

echo convertCurrency("10.00", "GBP", "USD");


Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!


My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.