PHP’s regex functions are all prefixed with preg_ and wrap PCRE (Perl-Compatible Regular Expressions) — the same regex engine used across countless other tools, so what you learn here transfers widely.
// Test a pattern
preg_match('/^\d{5}$/', '10115'); // 1 (German postal code format)
// Extract with capture groups
preg_match('/(\d{4})-(\d{2})-(\d{2})/', '2025-09-08', $matches);
// $matches = ['2025-09-08', '2025', '09', '08']
// Find all matches
preg_match_all('/#(\w+)/', 'Loving #php and #mysql today', $tags);
// $tags[1] = ['php', 'mysql']
// Replace
preg_replace('/\s+/', ' ', "too many spaces"); // "too many spaces"
// Replace with a callback
preg_replace_callback('/\{\{(\w+)\}\}/', fn($m) => strtoupper($m[1]), "Hello {{name}}");
Delimiters (usually /) wrap every PHP regex pattern — a common beginner mistake is forgetting them and getting a cryptic “no ending delimiter” warning.