PHP Regex means matching and changing text with PCRE (Perl Compatible Regular Expressions) through the preg_* functions. A pattern sits between delimiters (usually /.../), with optional modifiers after the closing delimiter (for example /hello/i for case-insensitive).
This page explains the ideas. The Regex Functions index lists all 11 function guides.
Why PCRE exists in PHP.
String helpers such as strpos() find fixed text. Regular expressions describe patterns—digits, emails-shaped tokens, repeated words—so one call can validate, extract, or rewrite many forms of input.
Delimiters are required.
Unlike some languages, PHP patterns are not bare strings of regex syntax alone. Always wrap the pattern: /^\d+$/, not ^\d+$. Choose a delimiter that does not collide with characters inside the pattern, or escape those characters.
Syntax
PHP Syntax
The usual pattern: write a delimited PCRE expression, then match, collect, replace, or quote:
PHP
// Delimiters + modifier (i = case-insensitive)
$ok = preg_match('/hello/i', 'Hello PHP'); // 1
// Capturing groups fill $matches
preg_match('/(\d{4})-(\d{2})-(\d{2})/', '2026-11-06', $m);
// $m[0] full match; $m[1], $m[2], $m[3] = year, month, day
// Every match
preg_match_all('/\b\w{4}\b/', 'this that word list', $all);
// Simple replace and quoted literal
$out = preg_replace('/\s+/', ' ', "a b\tc"); // "a b c"
$lit = preg_quote('price $5.00', '/'); // price \$5\.00
// Callback when each replacement needs PHP logic
$up = preg_replace_callback('/\b\w+\b/', function ($m) {
return strtoupper($m[0]);
}, 'hi php'); // HI PHP
Inspect the last PCRE error code and message after a failed call
Delimiters, modifiers, and callbacks
Common modifiers after the closing delimiter include i (ignore case), m (^/$ per line), s (dot matches newlines), and u (UTF-8). Prefer preg_replace() for fixed text and backreferences. Reach for preg_replace_callback() when each hit needs a function—formatting, lookups, or strtoupper() on a capture. After a false return, call preg_last_error_msg() to see what went wrong.
Map
Function Groups at a Glance
The Regex section is one family with four jobs. Use this map to pick the right group before you open a function page:
Group
Typical job
Start here
Match
Test for a match or collect every occurrence and capture groups
Neighboring tutorials that often sit next to this work: Strings for fixed-text search and replace, Filter for validating emails and integers without a custom pattern, Arrays when you walk preg_match_all() results, and Variables for storing matches and replacements.
Code
PHP Examples
Three short, offline-safe scripts: preg_match() with delimiters and groups, preg_match_all() for every hit, and preg_replace() plus preg_quote(). Each block includes a fixed View Output panel.
Example 1 — preg_match() with delimiters and groups
Use a delimited pattern with the i modifier, then capture year, month, and day from a date string.
collapsed: a b c
literal price found: yes
pattern: /price \$5\.00/
callback: HI PHP
How It Works
preg_replace('/\s+/', ' ', ...) turns runs of whitespace into a single space.
preg_quote() escapes $ and . so they match literally inside the pattern.
Pass the same delimiter (/) as the second argument so that character is escaped too if it appears.
preg_replace_callback() runs PHP for each word—use it when the replacement is not a fixed string.
Prefer plain preg_replace() for simple swaps; use callbacks only when you need custom logic.
Continue
Next: PHP Regex Functions Index
After you know delimiters, preg_match(), preg_match_all(), preg_replace(), preg_quote(), and when callbacks help, open the functions index for all 11 guides.
PHP uses PCRE (Perl Compatible Regular Expressions) through the preg_* functions. You write a pattern between delimiters (for example /.../), then match, replace, split, or filter strings with helpers such as preg_match(), preg_match_all(), and preg_replace().
Every PCRE pattern in PHP must start and end with the same delimiter character, usually a forward slash: /pattern/. You can use other characters (# or ~) when the pattern itself contains many slashes. Optional modifiers such as i (case-insensitive) go after the closing delimiter: /hello/i.
preg_match() looks for the first match and returns 1, 0, or false. preg_match_all() finds every match and fills an array with all of them. Use preg_match() for a yes/no check or the first capture; use preg_match_all() when you need every occurrence.
Call preg_quote() when a piece of the pattern comes from user input or a variable that may contain regex metacharacters (. * + ? [ ] and so on). It escapes those characters so they match literally instead of changing the pattern meaning.
Use preg_replace_callback() or preg_replace_callback_array() when each replacement must be computed in PHP—for example uppercasing a capture, looking up a map, or formatting numbers. Use plain preg_replace() when the replacement is a fixed string or simple backreferences like $1.
Start with preg_match() and a simple delimited pattern. Then learn preg_match_all() for multiple matches, preg_replace() for substitutions, and preg_quote() before you build patterns from variables. Open the functions index for all 11 guides.