Credit Card Number with Network Detection Regex for PHP
/^(?:(4)[0-9]{12}(?:[0-9]{3,6})?|(5[1-5][0-9]{14}|2(?:2[2-9][1-9]|[3-6][0-9]{2}|7[01][0-9]|720)[0-9]{12})|(3[47][0-9]{13})|(3(?:0[0-5]|[68][0-9])[0-9]{11})|(6(?:011|5[0-9]{2})[0-9]{12,15})|((?:2131|1800|35[0-9]{3})[0-9]{11}))$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching credit card number with network detection, ported and verified for PHP. Identity and credential patterns need both correctness and safety, since they're frequent targets for adversarial input. The snippet below is ready to drop into your PHP project — whether you're validating in a Laravel validator, a WordPress plugin, or a standalone PHP script.
Php Implementation
<?php
// Credit Card Number with Network Detection
// ReDoS-safe | RegexVault — Identity & PII > Financial PII
define('CREDIT_CARD_NUMBER_WITH_NETWORK_DETECTION_PATTERN', '/^(?:(4)[0-9]{12}(?:[0-9]{3,6})?|(5[1-5][0-9]{14}|2(?:2[2-9][1-9]|[3-6][0-9]{2}|7[01][0-9]|720)[0-9]{12})|(3[47][0-9]{13})|(3(?:0[0-5]|[68][0-9])[0-9]{11})|(6(?:011|5[0-9]{2})[0-9]{12,15})|((?:2131|1800|35[0-9]{3})[0-9]{11}))$/');
function validate_credit_card_number_with_network_detection(string $input): bool {
return (bool) preg_match(CREDIT_CARD_NUMBER_WITH_NETWORK_DETECTION_PATTERN, $input);
}
// Example
var_dump(validate_credit_card_number_with_network_detection("4111111111111111")); // bool(true)Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
4111111111111111 | 1234567890123456 |
5500005555555559 | 411111111111 |
378282246310005 | 411111111111111111111 |
30569309025904 | — |
6011111111111117 | — |
When to use this pattern
This pattern is drawn from the Identity & PII > Financial PII category and carries a ReDoS-safe certification. That matters for PHP developers because especially relevant in PHP where PCRE backtracking limits can trigger silent failures on malicious input. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
PCI-DSS requires Luhn validation, not just format matching. Storage of card data requires quarterly security assessments and annual PCI-DSS certification. Use a payment gateway (Stripe, Adyen) to avoid handling raw PANs.
Technical Notes
Capture groups: 1=Visa, 2=Mastercard, 3=Amex, 4=Diners Club, 5=Discover, 6=JCB. PCI-DSS: never store CVV, never log PANs, always encrypt stored card data, use tokenization for recurring billing.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern