+88 01797-506292

Post Date: May 30, 2017
Posted By: admin

Get Card Type By Number

function getCCType($cardNumber) {
// Remove non-digits from the number
    $cardNumber = preg_replace('/\D/', '', $cardNumber);

// Validate the length
    $len = strlen($cardNumber);
    if ($len < 15 || $len > 16) {
        throw new Exception("Invalid credit card number. Length does not match");
    }else{
        switch($cardNumber) {
            case(preg_match ('/^4/', $cardNumber) >= 1):
                return 'Visa';
            case(preg_match ('/^5[1-5]/', $cardNumber) >= 1):
                return 'Mastercard';
            case(preg_match ('/^3[47]/', $cardNumber) >= 1):
                return 'Amex';
            case(preg_match ('/^3(?:0[0-5]|[68])/', $cardNumber) >= 1):
                return 'Diners Club';
            case(preg_match ('/^6(?:011|5)/', $cardNumber) >= 1):
                return 'Discover';
            case(preg_match ('/^(?:2131|1800|35\d{3})/', $cardNumber) >= 1):
                return 'JCB';
            default:
                throw new Exception("Could not determine the credit card type.");
                break;
        }
    }
}

Card Number Validation

function validateChecksum($number) {

    // Remove non-digits from the number
    $number=preg_replace('/\D/', '', $number);

    // Get the string length and parity
    $number_length = strlen($number);
    $parity = $number_length % 2;

    // Split up the number into single digits and get the total
    $total=0;
    for ($i=0; $i<$number_length; $i++) {
        $digit=$number[$i];
        
        // Multiply alternate digits by two
        if ($i % 2 == $parity) {
            $digit*=2;
            
            // If the sum is two digits, add them together
            if ($digit > 9) {
                $digit-=9;
            }
        }
        
        // Sum up the digits
        $total+=$digit;
    }

    // If the total mod 10 equals 0, the number is valid
    return ($total % 10 == 0) ? TRUE : FALSE;

}

Leave a Reply

Your email address will not be published. Required fields are marked *