<?

/*

Validare de CNP

1) se calculeaza suma
s:=c(1)*2+c(2)*7+c(3)*9+c(4)*1+c(5)*4+c(6)*6+c(7)*3+c(8)*5+c(9)*8+c(10)*2+c(11)*7+c(12)*9
2) faci impartirea
rest=s%11
3) dupa cum:
- rest<10 at c(13)=rest
sau
- rest=10 at c(13)=1


*/

$cnp "";

if ( 
calcCNP $cnp ) ) {
    echo 
$cnp." e valid";
} else {
    echo 
$cnp." e invalid";
}

function 
calcCNP ($cnp) {

    if ( 
strlen ($cnp) != 13 ) {
        return 
FALSE;
    }

    
$s $cnp{0}*2+$cnp{1}*7+$cnp{2}*9+$cnp{3}*1+$cnp{4}*4+$cnp{5}*6+$cnp{6}*3+$cnp{7}*5+$cnp{8}*8+$cnp{9}*2+$cnp{10}*7+$cnp{11}*9;
    
$rest $s%11;
    if ( 
$cnp{12} == $rest ) {
        return 
TRUE;
    } else {
        return 
FALSE;
    }
}

?>