3
Mrz

Prüfziffer der ISIN in PHP

categories Computer, PHP    

Die ISIN (International Securities Identification Number) ist eine Kennung für Wertpapiere. Wie viele andere solcher Kennungen, beinhaltet auch die ISIN ein Prüfziffer-Verfahren. Wie das in Theorie funktioniert, hat jemand richtig gut bei Wikipedia beschrieben.

Hier ein kleine Klasse, wie man das in PHP machen kann (use with care and at own risk!).

/**
 * Validierung einer ISIN (double at double-method)
 * 2010 von Björn A. Dietz
 */
class isin_validate{
	public $isin;
	private $cvok;

	public function isin_check() {
		$isin = strtoupper($this->isin);
		$b = ""; $s="";	$cv = 0;
		$check = false;

		for	($i=0;$i<=10; $i++) {
			$b = substr($isin,$i,1);
			if (ord($b)>=65 and ord($b)<=90) $b = ord($b)-55;
			$s .= $b;
		}

		$s = strrev($s);

		for ($i=0;$i<=strlen($s);$i++) {
			$b = substr($s,$i,1);
			if (!bcmod($i,2)) $b = $this->quersumme($b * 2);
			$cv += $b;
		}

		$cv = 10 - bcmod($cv,10);
		if ($cv == substr($isin,-1,1)) $check = true;
		$this->cvok = $check;
		return $check;
	}

	private function quersumme($zahl) {
		$strZahl = (string) $zahl;
		for( $intQS = $i = 0; $i < strlen ($strZahl); $i++ ) {
			$intQS += $strZahl{$i};
		}
		return $intQS;
	}
}

…und so kann man die Klasse verwenden:

/* Anwendungsbeispiel */
$in = "DE000BAY0017";
$cv_test = new isin_validate();
$cv_test->isin = $in;
echo $cv_test->isin_check();

Ich weiß, das geht besser… so ist es aber schön übersichtlich!

Kommentare

Hinterlasse einen Kommentar!




WP SlimStat