SecureCreditCardFormatter

February 9, 2026 ยท View on GitHub

The SecureCreditCardFormatter formats and masks credit card numbers for secure display. It automatically detects card types, formats them appropriately, and masks sensitive portions.

Usage

Basic Usage

use Respect\StringFormatter\SecureCreditCardFormatter;

$formatter = new SecureCreditCardFormatter();

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

echo $formatter->format('341234567890123');
// Outputs: "3412 ****** 90123" (Amex, 4-6-5 format)

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

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

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

Custom Mask Character

use Respect\StringFormatter\SecureCreditCardFormatter;

$formatter = new SecureCreditCardFormatter('X');

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

Input Cleaning

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

use Respect\StringFormatter\SecureCreditCardFormatter;

$formatter = new SecureCreditCardFormatter();

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

API

SecureCreditCardFormatter::__construct

  • __construct(string $maskChar = '*')

Creates a new secure credit card formatter instance.

Parameters:

  • $maskChar: Character to use for masking (default: '*')

format

  • format(string $input): string

Formats and masks the input credit card number.

Parameters:

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

Returns: The formatted and masked credit card number

Masking

The formatter applies masking after formatting to ensure predictable positions:

Card TypeExample InputMask RangeOutput
Visa (16)41234567890123456-9,11-144123 **** **** 2345
Visa (19)41234567890123456786-9,11-14,16-194123 **** **** **** 678
MasterCard51123456789012346-9,11-145112 **** **** 1234
American Express3412345678901236-113412 ****** 90123
Discover60110009901394246-9,11-146011 **** **** 9424
JCB35280000123456786-9,11-143528 **** **** 5678
Diners Club (14)361234567890126-113612 ****** 9012
UnionPay62123456789012346-9,11-146212 **** **** 1234
RuPay81123456789012346-9,11-148112 **** **** 1234

Examples

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

Notes

  • Composes CreditCardFormatter for formatting and MaskFormatter for masking
  • Formats the card number first, then applies masking to the formatted string
  • Mask ranges are applied to 1-based positions in the formatted string
  • Non-digit characters are automatically removed from input
  • Inputs with fewer than 9 digits are returned as cleaned digits without formatting or masking
  • Uses CreditCardFormatter for card type detection and formatting