Codice PHP:
function ControllaPIVA($pi)
{
if( $pi == '' ) return '';
if( strlen($pi) != 11 )
return "La lunghezza della partita IVA non è\n"
."corretta: la partita IVA dovrebbe essere lunga\n"
."esattamente 11 caratteri.\n";
if( ! ereg("^[0-9]+$", $pi) )
return "La partita IVA contiene dei caratteri non ammessi:\n"
."la partita IVA dovrebbe contenere solo cifre.\n";
$s = 0;
for( $i = 0; $i <= 9; $i += 2 )
$s += ord($pi[$i]) - ord('0');
for( $i = 1; $i <= 9; $i += 2 ){
$c = 2*( ord($pi[$i]) - ord('0') );
if( $c > 9 ) $c = $c - 9;
$s += $c;
}
if( ( 10 - $s%10 )%10 != ord($pi[10]) - ord('0') )
return "La partita IVA non è valida:\n"
."il codice di controllo non corrisponde.";
return '';
}
l'ultimo return sta per true o false.
Io ho modificato quest'ultima funzione perchè mi serve una funzione che risponda true o false
Codice PHP:
function ControllaPIVA($pi)
{
if( $pi == '' ) return false;
if( strlen($pi) != 11 )
return false;
if( ! ereg("^[0-9]+$", $pi) )
return false;
$s = 0;
for( $i = 0; $i <= 9; $i += 2 )
$s += ord($pi[$i]) - ord('0');
for( $i = 1; $i <= 9; $i += 2 ){
$c = 2*( ord($pi[$i]) - ord('0') );
if( $c > 9 ) $c = $c - 9;
$s += $c;
}
if( ( 10 - $s%10 )%10 != ord($pi[10]) - ord('0') )
return false;
return ???;
}