CreditCardFormatter

February 9, 2026 ยท View on GitHub

The CreditCardFormatter formats credit card numbers with automatic card type detection. It supports major card networks including Visa, MasterCard, American Express, Discover, JCB, Diners Club, UnionPay, and RuPay.

Usage

Basic Usage with Auto-Detection

use Respect\StringFormatter\CreditCardFormatter;

$formatter = new CreditCardFormatter();

echo $formatter->format('4123456789012345');
// Outputs: "4123 4567 8901 2345" (Visa detected)

echo $formatter->format('371234567890123');
// Outputs: "3712 345678 90123" (Amex, 4-6-5 format)

echo $formatter->format('5112345678901234');
// Outputs: "5112 3456 7890 1234" (MasterCard detected)

echo $formatter->format('36123456789012');
// Outputs: "3612 345678 9012" (Diners Club, 4-6-4 format)

echo $formatter->format('4123456789012345678');
// Outputs: "4123 4567 8901 2345 678" (Visa 19-digit)

Input Cleaning

The formatter automatically removes non-digit characters from the input:

use Respect\StringFormatter\CreditCardFormatter;

$formatter = new CreditCardFormatter();

echo $formatter->format('4123-4567-8901-2345');
// Outputs: "4123 4567 8901 2345"

echo $formatter->format('4123 4567 8901 2345');
// Outputs: "4123 4567 8901 2345"

echo $formatter->format('4123.4567.8901.2345');
// Outputs: "4123 4567 8901 2345"

API

format

  • format(string $input): string

Formats the input credit card number.

Parameters:

  • $input: The credit card number (can include spaces, dashes, dots, etc.)

Returns: The formatted credit card number

Auto-Detection

The formatter automatically detects card type based on prefix and length:

Card TypePrefix RangesLengthFormat
American Express34, 3715#### ###### #####
Diners Club300-305, 309, 36, 3814#### ###### ####
Diners Club3616#### #### #### ####
Visa413, 16#### #### #### ####
Visa419#### #### #### #### ###
MasterCard51-55, 2221-272016#### #### #### ####
Discover6011, 644-649, 6516#### #### #### ####
Discover6011, 644-649, 6519#### #### #### #### ###
JCB3528-358916#### #### #### ####
JCB3528-358919#### #### #### #### ###
UnionPay6216#### #### #### ####
UnionPay6219#### #### #### #### ###
RuPay60, 65, 81, 82, 50816#### #### #### ####

Cards with more than 16 digits automatically use the 19-digit pattern: #### #### #### #### ###

Examples

InputOutputCard Type
41234567890123454123 4567 8901 2345Visa
41234567890123456784123 4567 8901 2345 678Visa (19)
51123456789012345112 3456 7890 1234MasterCard
3412345678901233412 345678 90123Amex
3712345678901233712 345678 90123Amex
60110009901394246011 0009 9013 9424Discover
35280000123456783528 0000 1234 5678JCB
361234567890123612 345678 9012Diners Club
62123456789012346212 3456 7890 1234UnionPay
81123456789012348112 3456 7890 1234RuPay
12345678901234561234 5678 9012 3456Unknown
4123-4567-8901-23454123 4567 8901 2345Visa (clean)

Notes

  • Non-digit characters are automatically removed from the input
  • Card type detection is based on card prefix and length (not Luhn validation)
  • If card type cannot be determined, uses the default 4-4-4-4 pattern
  • Uses PatternFormatter internally for formatting
  • For custom formatting patterns, use PatternFormatter directly