Jump to content

currency converter


lional

Recommended Posts

OK, this is actually quite simple.  First you have to decide:

A) Where's the content going to come from e.g. text file, website html, xml request to aggregator etc.
B) If you need to parse the value once it's returned.
C) How you want to output it.

I have a page that connects to Reuters website, gets a share price for a specific symbol, returns it to a script of mine and then tells me what my net share worth is, how many of each share I have, what I paid for them originally, and how much I've gained or lost on them.

Regards
Huggie
Link to comment
Share on other sites

What I would like to do is to get it for a website.
I am writing a site where the currency is in pounds. So what I would like to do is to capture the exchange rate for that day, write it to a field in the database, and when I run the script that needs the currency to be converted, it pulls the value from the database and does the conversion.

Thanks

Lional
Link to comment
Share on other sites

I just knocked this up.  It's messy, but it will give you an idea of how it's done.  You'd be better using an XML data feed if possible though.  There's plenty of them out there.

Regards
Huggie

[code]<?php
// URL of currency data
$url = "http://newsvote.bbc.co.uk/1/shared/fds/hi/business/market_data/currency/11/12/default.stm";

// Connect to the URL and pull the data down into a string
$raw_data = file_get_contents($url);

// Match the currency value
preg_match('/South African Rand.*?right">([^<]+)/is', $raw_data, $matches);

// Format to 2 decimal places
$ex_rate = number_format($matches[1], 2, '.', '');

// Total GBP
$p = "15.00";

// Echo some results
echo "The product you selected is £" . $p . " GBP, That's approximately " . number_format($p * $ex_rate, 2) . " ZRA*<br><br>";
echo "* price based on a rounded exchange rate of " . $matches[1];
?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.