Algorithms
|
|
|
|
<?php
function is_valid_zehut( $zehut ) {
/*
This function checks validity of Israeli interim ID number (teudat zehut)
See http://www.halemo.com/info/idcard for more info (in Hebrew).
The script has been thoroughly tested. However,
no warranties is given that it ever works.
The script is distributed under GPL v2.
****
Parameter: $zehut - 9 digits.
Use "012345674" to check.
Returns: true if number is correct, false otherwise
Tested under 4.3.2. Requires PHP 3.0.9
Author: Oleg Sverdlov <info@ols.co.il>
OLS Software http://www.ols.co.il
*/
function add10( $num ) {
// handles max 999
$d100 = floor($num/100);
$num = $num-($d100*100);
$d10 = floor($num/10);
$num = $num-($d10*10);
return $d100 + $d10 + $num;
};
if( strlen($zehut) > 9 ) {
return false;
}
$arrZehut = array();
if( preg_match( '/(d)(d)(d)(d)(d)(d)(d)(d)(d)/', $zehut, $arrZehut ) == 0 ) {
// Do not match
return false;
}
$pos = 1;
$s = 0;
for( $i=1; $i<=9; $i++ ) {
$s += add10( $arrZehut[$i] * $pos );
$pos = 3-$pos; // pos is 1-2-1-2-1-2-1-2-1
}
return (( $s % 10 ) == 0 );
} // end of is_valid_zehut()
?>
|
|
|
Usage Example
|
|
|
Rate This Script
|
|
|
|