Common Regex Patterns
A reference of frequently used PHP regex patterns for validation and text processing.
A reference of frequently used PHP regex patterns for validation and text processing.
<?php
// Email (RFC-compliant simplified)
$emailRegex = "/^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$/";
// URL
$urlRegex = "/^https?:\/\/[\w\-]+(\.[\w\-]+)+([\w.,@?^=%&:\/~+#\-]*)?$/i";
// Strong password
$passRegex = "/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&]).{8,}$/";
// ISO date YYYY-MM-DD
$dateRegex = "/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/";
// IPv4
$ipRegex = "/^(?:25[0-5]|2[0-4]\d|[01]?\d\d?)(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)){3}$/";
// PHP credit card (Luhn check separately)
$cardRegex = "/^\d{13,19}$/";
Regex validates format only — always perform additional business logic checks (Luhn for cards, MX lookup for emails).