Każda z poniższych funkcji na wejściu przyjmuje numer (w większości przypadków może on zawierać także spacje lub myślniki) a zwraca TRUE
lub FALSE
w zależności od tego czy numer jest poprawny czy też nie. Żadna filozofia.
PESEL
function check_pesel_number ($number)
{
$w = array(1, 3, 7, 9, 1, 3, 7, 9, 1, 3);
if (($l = strlen($number = preg_replace('/[^0-9]/', '', $number))) !== count($w) + 1) return false;
for ($s = $i = 0; $i < $l - 1; $i ++)
$s += $number[$i] * $w[$i];
return $number[$l - 1] == ((10 - ($s % 10)) % 10);
}
Code language: PHP (php)
NIP
function check_nip_number ($number)
{
$w = array(6, 5, 7, 2, 3, 4, 5, 6, 7);
if (($l = strlen($number = preg_replace('/[^0-9]/', '', $number))) !== count($w) + 1) return false;
for ($s = $i = 0; $i < $l - 1; $i ++)
$s += $number[$i] * $w[$i];
return (10 > ($c = ($s % 11))) && $number[$l - 1] == $c;
}
Code language: PHP (php)
REGON
function check_regon_number ($number)
{
$w = array(7 => array(2, 3, 4, 5, 6, 7), 9 => array(8, 9, 2, 3, 4, 5, 6, 7), 14 => array(2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8));
if (!isset($w[$l = strlen($number = preg_replace('/[^0-9]/', '', $number))])) return false;
for ($s = $i = 0; $i < $l - 1; $i ++)
$s += $number[$i] * $w[$l][$i];
return $number[$l - 1] == (($s % 11) % 10);
}
Code language: PHP (php)
Dowód Osobisty
function check_do_number ($number)
{
$t = 0;
$n = array();
$w = array(7, 3, 1);
if (!preg_match('/^([A-Z]+)(\d{1})(\d+)$/', strtoupper($number), $n)) return false;
foreach (str_split($n[1] . $n[3]) as $i => $l)
$t += $w[$i % 3] * (is_numeric($l) ? $l : (ord($l) - 55));
return ($t % 10) == (int)$n[2] ? true : false;
}
Code language: PHP (php)
PWZ
(Prawo Wykonywania Zawodu)
function check_pwz_number ($number)
{
if (strlen($number) != 7 || preg_match('/^0+$/', $number)) return false;
$c = (int)$number[0];
$n = substr($number, 1);
$s = 0;
for ($i = 1; $i <= 6; $i ++) $s += (int)$n[$i - 1] * $i;
return ($s % 11) == $c ? true : false;
}
Code language: PHP (php)
IBAN
(tylko dla polskich oddziałów banków)
function check_iban_number ($number)
{
$w = array(7, 1, 3, 9, 7, 1, 3);
$number = preg_replace('/[^A-Z0-9]/', '', !preg_match('/^([A-Z]{2})/', $number = strtoupper($number)) ? 'PL' . $number : $number);
if (preg_match('/^PL/', $number))
{
for ($s = $i = 0; $i < 7; $i ++)
$s += $number[4 + $i] * $w[$i];
if (($s % 10) != (int)$number[11])
return false;
}
$number = substr($number, 4) . substr($number, 0, 4);
foreach (str_split(substr(preg_replace_callback('/([A-Z])/', function ($match) { return (ord($match[0]) - 55); }, $number), 0, -2) . '00', 6) as $n)
$r = (int)((isset($r) ? $r : '') . $n) % 97;
return strcmp(sprintf('%02d', 98 - $r), substr($number, -2)) == 0;
}
Code language: PHP (php)
IMEI
function check_imei_number ($number)
{
$number = preg_replace('/[^0-9]/', '', $number);
for ($c = 0, $i = 0; $i < strlen($number) - 1; $i ++)
$c += array_sum(str_split($number[$i] * (($i + 1) % 2 == 0 ? 2 : 1)));
return (int)$number[strlen($number) - 1] === (10 - ($c % 10));
}
Code language: PHP (php)
Leave a Reply