Regular expressions let you search, match, and replace text using patterns. PHP uses preg_match(), preg_replace(), and preg_split() for regex. They are great for validating emails, phone numbers, and complex text patterns.
Regular Expressions
// Test if pattern matches
preg_match('/^\d{3}-\d{4}$/', "555-1234"); // 1 or 0
// Find all matches
preg_match_all('/\d+/', "abc123def456", $m);
print_r($m[0]); // ["123", "456"]
// Replace matches
preg_replace('/\s+/', ' ', "too many spaces");
// "too many spaces"
// Split by pattern
preg_split('/[\s,]+/', "one two,three");
// ["one", "two", "three"]
// Common patterns
'/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/' // email
'/^\+?[\d\s\-]{10,15}$/' // phone
'/^(?=.*[A-Z])(?=.*\d).{8,}$/' // strong password