Einfach in den Outputbereich eines Moduls kopieren - aktuelle Währungskurse werden per XML von der ECB geholt und geparsed (php4-domXML).
Code: Alles auswählen
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
function radioValue(rObj) {
for (var i=0; i<rObj.length; i++) if (rObj[i].checked) return rObj[i].value;
return false;
}
function selectValue(sObj) {
with (sObj) return options[selectedIndex].value;
}
function doCalc() {
if (!isNaN(substComma(document.umrechnung.betrag.value))) {
document.umrechnung.vonnach[0].checked ? document.getElementById('result').innerHTML=
runden(substComma(document.umrechnung.betrag.value)/selectValue(document.umrechnung.waehrung))
:
document.getElementById('result').innerHTML=
runden(substComma(document.umrechnung.betrag.value)*selectValue(document.umrechnung.waehrung));
}
}
function substComma(x) {
var p = x.indexOf(',');
if (p!=-1) {
x = x.substring(0, p) + '.' + x.substring(p+1, p+3);
}
return x;
}
function runden(x) {
var k = (Math.round(x * 100) / 100).toString();
k += (k.indexOf('.') == -1)? '.00' : '00';
var p = k.indexOf('.'), m = k.indexOf('-.');
var f = (p == 0 || m == 0)? '0,' : ',';
return k.substring(0, p) + f + k.substring(p+1, p+3);
}
//-->
</SCRIPT>
<div id=headbox>Währungsrechner</div><br>
<div id=convert>
<?php
class Currency {
var $uri="http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
var $pattern="/Cube currency='(.*)' rate='(.*)'/";
var $xmlString;
var $currencies;
function Currency() {
$this->xmlString=$this->loadXML();
$this->currencies=$this->currenciesRegex();
}
function loadXML() {
if ($this->url_exists($this->uri)) {
$handle = fopen ($this->uri, "r");
if($handle) {
while(!feof($handle)) {
$xmlString=$xmlString.fgets($handle,128);
}
fclose($handle);
}
}
return $xmlString;
}
function currenciesRegex() {
if($this->xmlString<>"") {
preg_match_all($this->pattern, $this->xmlString, &$hits,PREG_SET_ORDER);
foreach ($hits as $hit) {
$currencies[$hit[1]]=$hit[2];
}
}
return $currencies;
}
function url_exists($url) {
$a_url = parse_url($url);
if (!isset($a_url['port'])) $a_url['port'] = 80;
$errno = 0;
$errstr = '';
$timeout = 30;
if(isset($a_url['host']) && $a_url['host']!=gethostbyname($a_url['host'])){
$fid = fsockopen($a_url['host'], $a_url['port'], $errno, $errstr, $timeout);
if (!$fid) return false;
$page = isset($a_url['path']) ?$a_url['path']:'';
$page .= isset($a_url['query'])?'?'.$a_url['query']:'';
fputs($fid, 'HEAD '.$page.' HTTP/1.0'."\r\n".'Host: '.$a_url['host']."\r\n\r\n");
$head = fread($fid, 4096);
$head = substr($head,0,strpos($head, 'Connection: close'));
fclose($fid);
if (preg_match('#^HTTP/.*\s+[200]+\s#i', $head)) {
return true;
}
} else {
return false;
}
}
}
$currencies=new Currency();
?>
<form name="umrechnung" method="post" action="">
<select name="waehrung" id="waehrung">
<?php
foreach ($currencies->currencies as $key=>$value) {
?>
<option value="<?=$value;?>"><?=$key;?></option>
<?php
}
?>
</select>
<input name="betrag" type="text" id="betrag" size="6" maxlength="10" /><br>
<input name="vonnach" type="radio" value="in" checked />nach €
<input name="vonnach" type="radio" value="von" />von €<br> =
<span id="result">0</span><br>
<input name="calc" type="button" value="convert" onClick="doCalc();" />
</form>
</div>