Skip to content

Instantly share code, notes, and snippets.

@seyyah
Created July 24, 2010 08:55
Show Gist options
  • Select an option

  • Save seyyah/488551 to your computer and use it in GitHub Desktop.

Select an option

Save seyyah/488551 to your computer and use it in GitHub Desktop.
<?php
function is_valid ($tc) {
/* $tc, string secildi. Iki sebebi var:
1. Integer turunu ust siniri 10 haneli
echo PHP_INT_MAX; --> 2147483647
2. Double kullanilabilirdi, bu sefer de tamsayi - gercel
biraz daha karmasIk olacakti.
*/
$message = "";
if (empty($tc))
$message = "Boş olmamalı.";
else if (! ctype_digit($tc))
$message = "Tamsayı icermeli.";
else if (strlen($tc) <> 11)
$message = "Tam olarak 11 hane olmalı.";
else if (! is_tc($tc))
$message = "Geçerli bir TC no değil.";
return $message;
}
function is_tc($tc) {
// Kaynak: is_tc(): http://www.kodaman.org/yazi/t-c-kimlik-no-algoritmasi
preg_replace('/([1-9]{1})([0-9]{1})([0-9]{1})([0-9]{1})([0-9]{1})([0-9]{1})([0-9]{1})([0-9]{1})([0-9]{1}).*$/e', "eval('\$on=((((\\1+\\3+\\5+\\7+\\9)*7)-(\\2+\\4+\\6+\\8))%10); \$onbir=(\\1+\\2+\\3+\\4+\\5+\\6+\\7+\\8+\\9+\$on)%10; \$sonIki = \$on.\$onbir;')", $tc);
return(substr($tc, -2) == $sonIki);
}
/* TESTLER ... */
$tests = array("", "12.3", "123", "10000000145", "10000000146");
foreach ($tests as $tc) {
$message = is_valid($tc);
if (! empty($message) )
echo "TC = '{$tc}', message = {$message}\n<br />";
else
echo "TC = '{$tc}', GECERLI \n<br />";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment