• Iuri

    Nice code mate!
    Does it work with australian dollars as well?

  • Yes, I admire your “do it your way” style, and thanks for sharing. What your professional blog needs (and what I’ve seen on other technical blogs) is a deploy page for the examples, or an embedded area within the post, for dummies to see the exact result and the implementation.

  • Iuri: it works with any currency supported by Google’s conversion engine.
    Mihai: almost every post has a downloadable example. Just take it, unzip it and run it on your machine.

  • Mattew Morris

    Hi, any idea if there’s a way to force Google to return values to the nearest whole number? I’ve tried to use PHP’s “round” function, but it can’t deal with the commas that google returns to seperate thousands up. Thanks!

  • Mattew Morris

    Sorry, correction – Google returns a space after the thousand. “1300″ returns as “1 300″

  • Mattew Morris

    For anyone who’s interested – I solved my problem above by using the following:

    It replaces the space that Google returns after the “thousand” and then rounds down to the nearest whole number.

  • I’m glad you’ve find it usefull. Thank you for your positive feedback.

  • Well done Manic, thank you! I’ve edited this post and added a link to your comment!

  • Offedwimi> say what?

  • Naga sunford

    hello
    is any body to know the conversion of code in php from usd to Euro.becaZ im a php learner and beginner. so any body could help me

    FYI

    i have two text box 1 for dollar and another for euro .
    if type the value of us dollar in a text then click convert button it will automatically changed in Euro value in the another text box

  • Have you tried AJAX?

  • jaswant

    where is the script to download. Downlod link is not working.

  • NS

    I also got an error why I tried downloading the file. Can you correct this?

    Thanks.

  • Problem fixed, both of you!

  • Mattew Morris

    Hmm, the example above didn’t paste properly….

  • Manic

    Nice one, I used this to loop through a bunch of currency codes and get rates. I found that the function, in some cases, returns a value that is not a currency conversion.

    To solve this you need to add a pregmatch that checks to see if the resulting page is infact a conversion result page and not a standard google search result list.

  • Seb

    I have used the script but it returns the whole string after “= “.

    So I have modified the regular expression into:

    I am not an expert in regular expressions I tried to make it work with any digit, the white space, the “.” and the “,”.

    However, if you put large amounts google will return, for example, the word “million” and divide the result by one million, so this means that the function needs to be further “cleaned”.

    if you enter:

    1000000 gbp in usd

    Google returns:

    1 000 000 British pounds = 1.9866 million U.S. dollars

    any idea?

  • Yes, but I still have to parse the result with regular expressions. I want the converted value in one of my scripts…

  • In the end I used BNR’s official currency data (it’s only for RON):
    http://www.bnro.ro/nbrfxrates.xml

  • Yes, but this feed is quite recent. It’s only 2 or 3 weeks old 😛 At the time this article was written, the National Bank of Romania had a page that looked like it was developed in MS word or something similar and no XML feeds…

    Anyway, nice find 😛

  • Jrok

    Hey I’m having the same issue with removing the space when the ‘price’ is in the thousands, AND rounding it out to two decimal places. This is SO perfect for what I’m trying to do, but dang, not close enough!

    I’ve tried str_replace with  , and with %20; and I’m at a loss — please help!

  • Get the exchange rate for a single monetary unit and compute the price at your end:

  • Jrok

    GAH…. sometimes my brain gets stuck in a ‘rut’ of how to go about these things — thank you.

  • Lol, I hear you! It happens a lot to me too…

  • Hey, it seems to work perfect, but it is displaying 5 decimals. Is there any way that I can make it display only 2?

  • Nevermind, I just figured out, but thank you so much anyways. It works amazingly perfect

  • Mike VandeVelde

    Hi, this looks like it would work great, except that Google returns something like:

    403 ForbiddenGoogle Error ForbiddenYour client does not have permission to get URL /search?q=1+cad+in+eur from this server. (Client IP address: x.x.x.x) Please see Google’s Terms of Service posted at http://www.google.com/terms_of_service.html If you believe that you have received this response in error, please report your problem blah blah blah

    I guess they don’t like you hitting their search with a script? Have you ever seen this issue? Is this something they just started doing recently?

    Thanks for posting this anyway, but oh well back to the drawing board I guess…

  • I’ve ran the script again, this morning and it works…Could there be a problem at your end? Try it on a different IP…

  • laforge

    Very handy script. Thanks for this.

  • This is great!! Thanks.

    I just implemented the code to translate a fixed Euro product price into Danish krona, rounding to the nearest kr. Works beautifully. And so much simpler than anything else out there. Well done.

    Here’s the page: http://www.mindsparke.dk/buy.php

    Martin

  • I’ve looked over your site. You should really add a shareware version of the software on the site.

  • Hi Tudor.

    What makes you say that? I’m curious.

    Martin

  • Well, people might want to try it out before buying it. And while that might seem a bargain where you live, for some of us it isn’t.

    And when an offer is too good…well…it usually isn’t. I’m sure that I’m not the only one who would like to try it out before acquiring it.

  • Ah, I see. Thanks, Tudor.

    The program presents an unusual problem in this regard. It requires a good deal of commitment and effort on the part of the user before he or she would be able to say whether it worked for them or not. The user must train for no less than ten days before the results begin to appear. This is because it is stimulating changes in working memory and focus. The training is also particularly difficult for the first few days.

    This means that someone who purchases the software to perform the usual product evaluation (how does it look, what will it do for me) will find themselves immediately faced with the need to commit. (Reviewers have agreed with the conclusion that a trial version would be a bad idea.)

    Instead I offer the full, unconditional money-back guarantee, plus an additional $40 if someone commits to ten days and doesn’t find it useful. (I’ve had a few returns, but no one has yet taken me up on that ten day offer!!)

    If anyone contacts me wanting to try the software, I am always more than willing to supply a copy.

    Martin

  • Hi Guys,

    Here’s a much easier way:

    http://www.google.com/ig/calculator?hl=en&q=2.5AUD=?GBP

    It does output it as JSON, but it’s consistent and easilly parsable.

    Cheers,

    Lars

  • It wasn’t when this post was originally written, but it’s a much better way indeed. Great find Lars 😛

  • Pingback: Better PHP exchange rate script :: Get the exchange rate in PHP | Tudor Barbu's professional blog()

  • PS: another version of the script is available here.

  • sohail

    Plz tell me step by step how to use this script. I need to insert it in my html webpage.. please help..

  • Doesn’t work with HTML! You need PHP on the server…

  • sohail

    I hve PHP in my server.. I meant to say.. I need to embed this code in html page…

  • sohail

    Plz tell me step by step procedure how to use this script. I need to insert it in my html webpage.. please help . I hve PHP in my server.

  • Learn your PHP stuff! This blog is not for the absolute beginners. Basically all you have to do is call the exchangeRate function…

  • sohail

    I am not webmaster or sum thing. I am going to be professional in web related stuffs, but this is really urgent for me. I want to insert this converter in my non profit website. Please help.

  • Pingback: Currency Exchange with PHP | thisMayhem()

  • Suresh

    Thanks man for this code. it helped me a lot… 🙂

  • 1337ingDisorder

    Hi Tudor,

    This script has been working great for months but it seems to have broken today, I suspect Google may have changed the format of their output or something to that effect.

    The error output it’s now giving is as follows:

    PHP Notice: Undefined offset: 1 in /path/to/currency_converter.php on line 21

    This happened with the usual cron-triggered execution at 8:30am this morning and is still happening when I try manually now (it’s now around noon).

    Will try again later in case it’s just a Google hiccup but you may want to try yourself to see if you get the same results or whether it’s just busted for my deployment. 🙂

    Thanks again for writing this script in the first place, has kept my bosses happy for a long time so far!

    — 1337ingDisorder

  • 1337ingDisorder

    Cancel my last post, the currency conversion is actually working fine. Our problem is that a different script (based on Apple reports, which have changed their format) started passing bad variables to the currency converter.

    Sorry for the false alarm and thanks again for this handy script! 🙂

    — 1337ingDisorder

  • Gamlastan

    Hi, thanks for this, it was very useful!! I was using an external currency converter calculator from http://currencycrab.com which was ok.. but this way i will keep it cleaner.. cheers! 🙂

Advertisment ad adsense adlogger