Created
December 11, 2008 14:29
-
-
Save neves/34726 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<? | |
function validar_cartao($c) { | |
$c = ereg_replace("[^0-9]", "", $c); | |
$s = 0; | |
$v = ''; | |
$n = strlen($c); | |
if ($n != 16) return false; | |
if ($c[0] != '4') return false; | |
for($i = 0; $i < $n; $i += 2){ | |
$s = $c[$i]*2; | |
if ($s > 9) { | |
$s = substr($s, 0, 1) + substr($s, 1, 1); | |
} | |
$v .= "$s" . $c[$i+1]; | |
} | |
$s = 0; | |
for($i = 0; $i < $n; $i++){ | |
$s += $v[$i]; | |
} | |
if ($s > 150) return false; | |
if ($s % 10 > 0) return false; | |
return true; | |
} | |
// NÚMEROS CARTÕES PARA TESTE | |
$cartoes = array(); | |
$cartoes["amex"] = "376411112222331"; | |
$cartoes["mastercard"] = "5555666677778884"; | |
$cartoes["diners"] = "30111122223331"; | |
$cartoes["visa"] = "4073020000000002"; | |
$cartoes["visa2"] = "4111111111111111"; | |
$cartoes["visa3"] = "4012888888881881"; | |
$cartoes["visa4"] = "4222222222222"; | |
$cartoes["hipercard"] = "3841001111222233334"; | |
$cartoes["aura"] = "5078601870000127985"; | |
$cartoes["aura2"] = "5078601800003247449"; | |
$cartoes["American Express"] = "378282246310005"; | |
$cartoes["American Express2"] = "371449635398431"; | |
$cartoes["American Express Corporate"] = "378734493671000"; | |
$cartoes["Australian BankCard"] = "5610591081018250"; | |
$cartoes["Diners Club"] = "30569309025904"; | |
$cartoes["Diners Club2"] = "38520000023237"; | |
$cartoes["Discover"] = "6011111111111117"; | |
$cartoes["Discover2"] = "6011000990139424"; | |
$cartoes["JCB"] = "3530111333300000"; | |
$cartoes["JCB2"] = "3566002020360505"; | |
$cartoes["MasterCard"] = "5555555555554444"; | |
$cartoes["MasterCard2"] = "5105105105105100"; | |
// TESTA E EXIBE OS QUE FALHARAM | |
foreach($cartoes as $bandeira => $numero): | |
if (!validar_cartao($numero)) | |
echo "$bandeira: $numero\n"; | |
endforeach; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment