Skip to content

Instantly share code, notes, and snippets.

@dagostoni
Last active December 28, 2015 16:49
Show Gist options
  • Save dagostoni/7531058 to your computer and use it in GitHub Desktop.
Save dagostoni/7531058 to your computer and use it in GitHub Desktop.
Verifica Codice Fiscale
<?php
function checkTaxCodeIt($tax_code){
$odd = array('0' => 1, '1' => 0, '2' => 5, '3' => 7, '4' => 9, '5' => 13, '6' => 15, '7' => 17, '8' => 19, '9' => 21, 'A' => 1, 'B' => 0, 'C' => 5, 'D' => 7, 'E' => 9, 'F' => 13, 'G' => 15, 'H' => 17, 'I' => 19, 'J' => 21, 'K' => 2, 'L' => 4, 'M' => 18, 'N' => 20, 'O' => 11, 'P' => 3, 'Q' => 6, 'R' => 8, 'S' => 12, 'T' => 14, 'U' => 16, 'V' => 10, 'W' => 22, 'X' => 25, 'Y' => 24, 'Z' => 23);
$even = array('0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5, 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11, 'M' => 12, 'N' => 13, 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17, 'S' => 18, 'T' => 19, 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23, 'Y' => 24, 'Z' => 25);
$tax_code = strtoupper($tax_code);
if($tax_code == '') return false;
if(strlen($tax_code) != 16) return false;
if(!preg_match("/[A-Z0-9]+$/", $tax_code)) return false;
$tax_code = str_split($tax_code);
$x = 0;
$y = 0;
$i = 1;
foreach($tax_code as $n){
if($i <= 15){
if($i % 2 == 0){
$y += $even[$n];
} else {
$x += $odd[$n];
}
}
$i++;
}
$code = ($x+$y) % 26;
$control = chr($code+65);
if( $tax_code[15] != $control){
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment