Skip to content

Instantly share code, notes, and snippets.

@dagostoni
Created November 18, 2013 16:44
Show Gist options
  • Save dagostoni/7531076 to your computer and use it in GitHub Desktop.
Save dagostoni/7531076 to your computer and use it in GitHub Desktop.
Verifica Partita Iva
<?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