Created
November 18, 2013 16:44
-
-
Save dagostoni/7531076 to your computer and use it in GitHub Desktop.
Verifica Partita Iva
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function checkVatIt($vat){ | |
if($vat == '') return false; | |
if(strlen($vat) != 11) return false; | |
if(!preg_match("/^[0-9]+$/", $vat)) return false; | |
$vat = str_split($vat); | |
$x = 0; | |
$y = 0; | |
$i = 1; | |
foreach($vat as $n){ | |
if($i <= 10){ | |
if($i % 2 == 0){ | |
$y += ((2 * $n) > 9)? (2 * $n) - 9: 2 * $n; | |
} else { | |
$x += $n; | |
} | |
} | |
$i++; | |
} | |
$t = ($x + $y) % 10; | |
$control = (10 - $t) % 10; | |
if( $vat[10] != $control){ | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment