Web Design and Web Development Forum

  1. #1
    Join Date
    Jan 2005
    Posts
    9
    Rep Power
    0
  2. parabola is on a distinguished road
  3. PHP Currency Conversion Script

    Hi! I need to get a currency conversion script done. I want to make my own exchange rate.

    This how id like it to work: User inputs amount in Currency 1, presses submit. It outputs the converted amount using my own exchange rate.

    Can anyone tlel me coding how to do this?

    Thanks!
    Last edited by Shane; 01-19-2005 at 06:08 AM. Reason: Let's make the title a little more informative.
    Reply With Quote Reply With Quote
  4. #2
    Shane's Avatar
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Age
    24
    Posts
    1,163
    Rep Power
    9
  5. Shane is on a distinguished road
  6. Re: Conversion

    What would "Currency 1" be? Your own made up number? Or would it go off real currencies, Eg. USD?
    Reply With Quote Reply With Quote
  7. #3
    Join Date
    Jan 2005
    Posts
    9
    Rep Power
    0
  8. parabola is on a distinguished road
  9. Re: Conversion

    I'm looking to convert USD to AUD.
    Reply With Quote Reply With Quote
  10. #4
    Shane's Avatar
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Age
    24
    Posts
    1,163
    Rep Power
    9
  11. Shane is on a distinguished road
  12. Re: Conversion

    Here you go, you can embed this into a PHP script rather easly.

    I did a quick script that depends on XE.com's converter (and it also depends that the html code for the converter doesn't change very much, if so, the script won't work, LOL.), check out a live demo here:

    http://demo.matrixau.net/currency_co...o=aud&amount=5

    How to use?
    Easy. It's all in the address, like you see in my demo:

    http://demo.matrixau.net/currency_convert/convert.php?from=usd&to=aud&amount=5

    Code for the script:

    PHP Code:
    <?php

    //*****************************************
    //This rather ugly code is done by Shane  *
    //   Contact Shane: shane@matrixau.net    *
    //  Exclusively for YoungCoders.com!      *
    //                                        *
    // PLEASE DON'T REMOVE THIS IF POSSIBLE!  *
    //*****************************************

    //Get file's contents
    $content file_get_contents("http://www.xe.com/ucc/convert.cgi?From=" .$_GET['from']. "&To=" .$_GET['to']. "&Amount=" .$_GET['amount']. "&template=pca-xetrade");

    //Define the HTML code we have to break down with a uniqe peice of HTML code found in that page.
    $content explode('<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0>',$content);

    //We will get rid of this line to get rid of the left over code above the bits we want to get to.
    $content str_replace('<TD VALIGN=MIDDLE ALIGN=RIGHT><FONT FACE="Arial,Helvetica"><B>'"""$content[1]");

    //Once again defining our way to the final converted number...
    $content explode('<TD COLSPAN=3 ALIGN=CENTER><FONT FACE="Arial,Helvetica" SIZE=-2>',$content);

    //Remove every bit of HTML code left that isn't numeric!
    $toremove = array("a""b""c""d""e""f""g""h""i""j""k""l""m""n""o""p""q""r""s""t""u""v""w""x""y""z""A""B""C""D""E""F""G""H""I""J""K""L""M""N""O""P""Q""R""S""T""U""V""W""X""Y""Z"">""<""\"""\'""="",""/""-1");
    $content str_replace($toremove"""$content[0]");

    //Final thing to remove, Woo Hoo! We will use '+1' to sperate the two numeric values of the two currencys.
    $final explode('+1',$content);

    //Make the currency codes in upper case...
    $from_code strtoupper($_GET['from']);
    $to_code strtoupper($_GET['to']);

    //The final conversion
    echo '<b>' .$final[0]. ' ' .$from_code' =' .$final[1]. ' ' .$to_code'</b>';

    ?>
    Feel free to make comments on my messy code. :p.

    It works great and isn't as slow as I expected it would be, it's actally quite fast...

    Enjoy!

    - Shane.
    Last edited by Shane; 05-29-2005 at 09:35 AM.
    Reply With Quote Reply With Quote
  13. #5
    Join Date
    Jan 2005
    Posts
    9
    Rep Power
    0
  14. parabola is on a distinguished road
  15. Re: Conversion

    Thanks, but the reason i wanted to set the amount is because people may try and cheat me out of money by saying "But the exchange rate was number that day"

    When it could be something else.
    Reply With Quote Reply With Quote
  16. #6
    Shane's Avatar
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Age
    24
    Posts
    1,163
    Rep Power
    9
  17. Shane is on a distinguished road
  18. Re: Conversion

    Quote parabola originally posted:
    Thanks, but the reason i wanted to set the amount is because people may try and cheat me out of money by saying "But the exchange rate was number that day"

    When it could be something else.
    So, is that script I made what you needed?
    - Shane :cool:
    YC Admin

    MirrorMAX Mirroring Service | MatrixAu
    Need website hosting or services hosted? Try contacting me. :)
    Reply With Quote Reply With Quote
  19. #7
    Join Date
    Jan 2005
    Posts
    9
    Rep Power
    0
  20. parabola is on a distinguished road
  21. Re: Conversion

    Is it possible to make it email an address with their IP and the converted rate? So when they pay I can check they payed the proper amount.
    Reply With Quote Reply With Quote
  22. #8
    Shane's Avatar
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Age
    24
    Posts
    1,163
    Rep Power
    9
  23. Shane is on a distinguished road
  24. Re: Conversion

    Sure it's possible.

    But wait, do you know any PHP? :p. You should be able to do some of this yourself if you know a bit, if you don't you should learn some basics :D.
    - Shane :cool:
    YC Admin

    MirrorMAX Mirroring Service | MatrixAu
    Need website hosting or services hosted? Try contacting me. :)
    Reply With Quote Reply With Quote
  25. #9
    Join Date
    Jan 2005
    Posts
    9
    Rep Power
    0
  26. parabola is on a distinguished road
  27. Re: Conversion

    Ahh I know a tincy bit. This is basically advanced for me :P
    Reply With Quote Reply With Quote
  28. #10
    Shane's Avatar
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Age
    24
    Posts
    1,163
    Rep Power
    9
  29. Shane is on a distinguished road
  30. Re: Conversion

    Quote parabola originally posted:
    Ahh I know a tincy bit. This is basically advanced for me :P
    Ok, lol. Well I still don't understand exactly what you need done... what is it for exactly?
    - Shane :cool:
    YC Admin

    MirrorMAX Mirroring Service | MatrixAu
    Need website hosting or services hosted? Try contacting me. :)
    Reply With Quote Reply With Quote

Similar Threads

  1. PHP Tutorial for beginners
    By YoungCoder in forum PHP Articles
    Replies: 19
    Last Post: 04-23-2006, 02:31 PM
  2. What is PHP?
    By wizard in forum PHP Articles
    Replies: 4
    Last Post: 11-27-2005, 06:38 AM
  3. Php Syntax
    By wizard in forum PHP Articles
    Replies: 1
    Last Post: 10-06-2005, 01:53 AM
  4. PHP Manual [Chris's Version]
    By Chrishasfun in forum PHP Articles
    Replies: 0
    Last Post: 01-02-2005, 02:22 PM
  5. PHP News Script
    By unclekyky in forum PHP Articles
    Replies: 0
    Last Post: 09-03-2004, 08:07 PM