mike_mech: dzieki za wskazenie drogi co do tematu posta

powyzsza funkcja zyskala poszerzona funkcjonalnosc, i teraz sprawdza poprawnosc numerow: NIP, REGON, PESEL, zmienila tez nazwe, poprawiłem tez małe niedopatrzenie .
Cytat
if ($nr === NULL && empty($nr)) return false;
poprawiono na
<?php
($nr === NULL || empty($nr)) ?>
oki, kod:
<?php
function is_evidence_nr($nr, $type=0)
{
if ($nr === NULL || empty($nr)) return false;
$types = array(0 => "NIP", 1 => "REGON", 2 => "PESEL");
else $type = (string) $types[$type];
$weights["NIP"] = array (6
, 5
, 7
, 2
, 3
, 4
, 5
, 6
, 7
) ; $weights["REGON_7"] = array (2
, 3
, 4
, 5
, 6
, 7
) ; $weights["REGON_9"] = array (8
, 9
, 2
, 3
, 4
, 5
, 6
, 7
) ; $weights["PESEL"] = array (1
, 3
, 7
, 9
, 1
, 3
, 7
, 9
, 1
, 3
);
$mods["NIP"] = (int) 11;
$mods["REGON"] = (int) 11;
$mods["PESEL"] = (int) 10;
{
$digits = $digits[0];
if ($type == "REGON") {
$digits_count = count($digits); if ($digits_count != 9 && $digits_count != 7) return false;
$idx = (string
) sprintf("%s_%d", $type, $digits_count); }
else $idx = (string) $type;
$weights = $weights[$idx];
$mods = $mods[$type];
if ( count($digits)-1
!= count($weights) ) return false; $all_sum = (int) 0;
foreach ( $digits as $digit )
{
$all_sum += (int) $digit * $weight;
}
$calc_ctrl = (int) ($all_sum % $mods == 10) ? 0 : $all_sum % $mods;
if ($type == "PESEL") $calc_ctrl = (int) (10 - ($all_sum % $mods) == 10) ? 0 : 10 - ($all_sum % $mods) ;
if ($calc_ctrl == $ctrl_digit) return true;
}
return false;
}
?>
uzycie :
Kod
sprawdza poprawność podanego numeru NIP, REGON lub PESEL
bool is_evidence_nr(string nr, [int typ] )
typ to 0,1 lub 2 odpowiednio dla NIP, REGON lub PESEL
domyslnie typ = 0, czyli NIP