21, November 2008

Inline PHP currency conversion - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Inline PHP currency conversion
« previous next »
Pages: [1] Print

Author Topic: Inline PHP currency conversion  (Read 617 times)
Tim Nash
Global Moderator
Community Supporter ?
Internet Junkie
*****
Posts: 2173
5036 credits
Members referred : 2


Venture Skills - New Media & IT group


« on: Mar 10, 2007, 11:27:59 PM »

I had need for this a few days ago, and in the end hacked together this version, it uses CURL and calls Google to get current currencies it can be easily modified, so if people fancy making variants post them up.

so exchange.php
Code:
<?php
function exchangeRate$amount$currency$exchangeIn )
{
    
$googleQuery $amount ' ' $currency ' in ' $exchangeIn;
    
$googleQuery urlEncode$googleQuery );
$ch curl_init(); //set curl variable
$timeout 10// set to zero for no timeout
curl_setopt ($chCURLOPT_URL'http://www.google.com/search?q=' $googleQuery); //call to Google Servers
curl_setopt ($chCURLOPT_RETURNTRANSFER1);
curl_setopt ($chCURLOPT_CONNECTTIMEOUT$timeout);
$file_contents curl_exec($ch);
$file_contents strip_tags$file_contents );
curl_close($ch);
 
$matches = array();
    
preg_match'/= (([0-9]|\.|,|\ )*)/'$file_contents$matches );
    return 
$matches[1] ? $matches[1] : false;
}
?>


And to call the function simply include the following in your page
Code:
<?php require( 'path/to/exchange.php' ); echo '£100 ($' exchangeRate100'GBP''usd' ) . '  ,€' exchangeRate100'GBP''euro' ). ')'?>

its pretty straight forward,
exchangeRate( 100, 'GBP', 'usd' )
In this example the 100 is the value, the GBP is the default currency and usd is the currency to convert.

It is simple but hopefully it will aid some one Smiley

Would you like to be an SEO, let me help with, The Tim Nash introduction to SEO Visit through proxy alternatively for Social media optimisation take a look at the Venture Skills Blog Visit through proxy

Last blog : Its all in the mp3s
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8245
42457 credits
Members referred : 3



« Reply #1 on: Mar 10, 2007, 11:38:16 PM »

Nice snippet Smiley

Feel free to post it in the tutorial section Visit through proxy Wink

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Tim Nash
Global Moderator
Community Supporter ?
Internet Junkie
*****
Posts: 2173
5036 credits
Members referred : 2


Venture Skills - New Media & IT group


« Reply #2 on: Mar 11, 2007, 12:24:28 AM »

I will write up a more detailed tutorial tomorrow and put it in. Its a nice intro to using Google services and CURL so would make a nice programming tutorial

Would you like to be an SEO, let me help with, The Tim Nash introduction to SEO Visit through proxy alternatively for Social media optimisation take a look at the Venture Skills Blog Visit through proxy

Last blog : Its all in the mp3s
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6437
39446 credits
Members referred : 374


It's time to use PHP5!


« Reply #3 on: Mar 12, 2007, 09:41:26 AM »

Thanks for sharing this usefull function


Last blog : Just a better Internet portal provided by Google
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #4 on: Mar 12, 2007, 12:51:29 PM »

excellent piece of code! I'm updating conversion rates manually on one of my websites every week (if I remember to do the update that is Wink ) This code will be used instead from now on!


Last blog : Are You Stumbling Yet?
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=6197
Tags : php currency coversion Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Inline PHP currency conversion
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Nov 21, 2008, 10:43:10 AM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!





Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.