// A function that will accept and clean up CC numbers functionstandardize_credit($num) { // Remove all non-digits from the string returnpreg_replace('/[^0-9]/', '', $num); } // A function to check the validity of a CC number // It must be provided with the number itself, as well as // a character specifying the type of CC: // m = Mastercard, v = Visa, d = Discover, a = American Express functionvalidate_credit($num, $type) { // First perform the CC specific tests: // Store a few evaluations we will need often: $len = strlen($num); $d2 = substr($num,0,2); // If Visa must start with a 4, and be 13 or 16 digits long: if ( (($type == 'v') && (($num{0} != 4) || !(($len == 13) || ($len == 16)))) || // If Mastercard, start with 51-56, and be 16 digits long: (($type == 'm') && (($d2 < 51) || ($d2 > 56) || ($len != 16))) || // If American Express, start with 34 or 37, 15 digits long: (($type == 'a') && (!(($d2 == 34) || ($d2 == 37)) || ($len != 15))) || // If Discover: start with 6011 and 16 digits long (($type == 'd') && ((substr($num,0,4) != 6011) || ($len != 16))) ) { // Invalid card: returnfalse; } // If we are still here, then time to manipulate and do the Mod 10 // algorithm. First break the number into an array of characters: $digits = str_split($num); // Now reverse it: $digits = array_reverse($digits); // Double every other digit: foreach(range(1, count($digits) - 1, 2) as$x) { // Double it $digits[$x] *= 2; // If this is now over 10, go ahead and add its digits, easier since // the first digit will always be 1 if ($digits[$x] > 9) { $digits[$x] = ($digits[$x] - 10) + 1; } } // Now, add all this values together to get the checksum $checksum = array_sum($digits); // If this was divisible by 10, then true, else it's invalid return (($checksum % 10) == 0) ? true : false; } // Check various credit card numbers: $nums = array( '344 2345 3466 4577' => 'a', '3794 2345 3466 4577' => 'a', '4938748398324' => 'v', '4123-1234-5342' => 'v', '51847293 84567434' => 'm', '5723x2345x2345x6161' => 'm', '6011 6011 6011 6011' => 'd', '6012 392563242423' => 'd', ); foreach ($numsas$num => $type) { $st = standardize_credit($num); $valid = validate_credit($st, $type); $output = $valid ? 'Valid' : 'Invalid'; echo"<p>{$st} - {$type} = {$output}</p>/n"; }