Location: PHPKode > scripts > Money Currency > money-currency/moneycurrency.class.php
<?php
	class MoneyAPI
	{
		// Function to open stream
		function open()
		{
			// Try to open a XML stream
			if($stream = simpleXML_load_file('http://www.nationalbanken.dk/dndk/valuta.nsf/valuta.xml'))
			{
				// If true, then return resource
				return($stream);
			}
			else
			{
				// Else return false
				return(false);
			}
		}

		// Function to get current rate for money code
		function get_rate_for($stream, $code)
		{
			// If stream was false
			if(!$stream)
			{
				// Then return false
				return(false);
			}
			else
			{
				// Fetch money codes and rates
				$ver = $stream->dailyrates->currency;

				// Set stop
				$stop = false;

				foreach($ver as $m)
				{
					// Compare code
					if($stop)
					{
					}
					elseif($m['code'] != $code)
					{
						// Set current return as false
						$return = false;
						$stop = false;
					}
					else
					{
						// Set code if match
						$return = $m['rate'];

						// Set stop
						$stop = true;

						// Return result
						return($return);
					}
				}

				if(!$return)
				{
					// Return result
					return(false);
				}
			}
		}

		function convert($stream, $from, $to, $total, $way = NULL, $after = 2)
		{
			// If stream was false
			if(!$stream)
			{
				// Then return false
				return(false);
			}
			else
			{
				// Otherwise get money rates and replace american . to ,
				$value_one = str_replace(',', '..', $this->get_rate_for($stream, $from));
				$value_two = str_replace(',', '..', $this->get_rate_for($stream, $to));

				$value_one = str_replace('.', '', $value_one);
				$value_two = str_replace('.', '', $value_two);

				$value_one = str_replace('..', '.', $value_one);
				$value_two = str_replace('..', '.', $value_two);

				// Do some mathematical calculations
				$return = (($total * $value_one) / $value_two);

				// Return extra integers after a . 
				$ep = explode('.', $return);
				$ex = substr($ep[1], (strlen($ep[1]) - 2));

				$return = $ep[0] . '.' . substr($ep[1], 0, $after);

				// Replace to American format
				if(strtolower($way) != 'american')
				{
					// If not american format
					$return = str_replace('.', ',', $return);
				}

				// Return it!
				return($return);
			}
		}
	}
?>
Return current item: Money Currency