Location: PHPKode > scripts > PMC_IBAN > pmc_iban/PMC_IBAN.php
<?php
/** 
 * PMC_IBAN class
 * 
 * This class generates the IBAN (ISO-Norm 13616) for the most 
 * European countries
 * 
 * @author Markus Pfeifenberger <www.pfeifenberger.com>
 * @author www.pmcweb.at <www.pmcweb.at>
 * @since 10.07.03 13:49:46 
 * @version 20030710
 *
 **/
class PMC_IBAN{
	// Array mit den Ländereinstellungen
	var $arr_IBAN = array();
	// file with the iban array for the different countries
	var $file_arr_iban = "iban.arr.php";

	/**
     * Constructor
	 * 
	 * @param string $arr_file the file with the IBAN array
     * @access protected
     */
	function PMC_IBAN($file_arr_iban=""){
		if ($file_arr_iban!="") {
		    $this->file_arr_iban = $file_arr_iban;
		}
		$this->_setArrayIBAN();		
	}

	/**
	 * get the IBAN (ISO-Norm 13616)
	 * 
	 * @param string $kto Kontonummer
	 * @param string $blz Bankleitzahl
	 * @access public
	 * @return void 
	 **/
	function getIBAN($kto, $blz, $land, $formated=1) {		
 	    $postfix = $this->_getPostfix($land);

		$blz = str_pad($blz, $this->arr_IBAN[$land]["blz"], "0", STR_PAD_RIGHT);
		$kto = str_pad($kto, $this->arr_IBAN[$land]["kto"], "0", STR_PAD_LEFT);			
	 	$code = $blz.$kto.$postfix;
		
		$pr = 98 - $this->mod97($code);
		$pr  = str_pad($pr, 2, "0", STR_PAD_LEFT);
		
		$ret = $land.$pr.$blz.$kto;
		
		if ($formated) {	    
			unset($r);
			for($i = 0; $i < strlen($ret); $i++){
				$r .= $ret[$i];
				$r .= ($i%4==3) ? " " : "";
			}
			$ret = $r;
		}
		
		return $ret;
	}
	
	/**
	 * get the postfix of the IBAN
	 * This is a number of the country and "00"
	 * @access private
	 * @return void 
	 **/
	function _getPostfix($land){		
		return $this->_getCountryCodeNumber($land)."00";			
	}
	
	/**
	 * Transfers the 2 letter country code (eg "AT" for Austria) to
	 * an integer number 
	 * 
	 * Conversion: A .. 10, B .. 11, ..., Z.. 35 (uses ord("A") = 65 )
	 * @access private
	 * @return integer number of a country code
	 **/
	function _getCountryCodeNumber($land){
		return (ord($land[0])-55).(ord($land[1])-55);		
	}
	
	/**
	 * validate the IBAN
	 * The modala 97 value should be 1
	 * 
	 * 
	 * @access public
	 * @return void 
	 **/
	function validateIBAN($IBAN){	
		$val = str_replace(" ", "", $IBAN);
		$val = substr($val, 4).$this->_getCountryCodeNumber(substr($val, 0, 2)).substr($val, 2, 2);		
		$ret = ( $this->mod97($val)==1 ) ? TRUE : FALSE;
		return $ret;
	}
	
	
	/**
	 * calculates modula 97 of a given (big) integer number,
	 * where the number%97 does not work
	 * 
	 * @param string $str a big integer number
	 * @access private
	 * @return integer the modula 97 value of $str
	 **/
	function mod97($str){
		unset($ret);
		while($str!=""){
			if (!isset($ret)) { // erster durchlauf
	  		    $ret = substr($str, 0, 9);			
				$str = substr($str, 9);
			} else {
				$lenght = 9 - strlen($ret);			
				$ret .= substr($str, 0, $lenght);
				$str = substr($str, $lenght);						
			} 		
			$ret = $ret%97;
		}	
		
		return $ret;
	}
	
	/**
	 * set the IBAN array contained in a file
	 * 
	 * @access private
	 * @return void 
	 **/
	function _setArrayIBAN(){
		if (@include($this->file_arr_iban)) {
			$this->arr_IBAN = $arr;
			unset($arr);    
		} else {
			echo "ERROR: IBAN array file is not set!!!";			
			exit;
		}		
	}
}
?>
Return current item: PMC_IBAN