PHP Regex Functions

About This Index
This page is the PHP Regex Functions index for the tutorial track. Each row links to a full guide with syntax, examples, and FAQs.
Learn delimiters, preg_match(), preg_match_all(), preg_replace(), preg_quote(), and callbacks on /php/regex. Use this page when you need a specific function such as preg_match(), preg_replace(), or preg_quote().
Start with preg_match() and a delimited pattern—then learn preg_match_all() for every hit, preg_replace() for substitutions, and preg_quote() before building patterns from variables.
Every Regex function in this series appears below in four categories: Match, Replace & filter, Split & grep, and Quote & errors.
How to Call a PHP Regex Function
Regex helpers are global functions—call them directly with a delimited PCRE pattern:
$ok = preg_match('/\d+/', 'order 42', $m); // 1; $m[0] === '42'
preg_match_all('/\w+/', 'a b c', $all); // 3 matches
$out = preg_replace('/\s+/', ' ', "x y"); // "x y"
$lit = preg_quote('$5.00', '/'); // \$5\.00
$parts = preg_split('/,\s*/', 'a, b, c'); // ['a','b','c']
$hits = preg_grep('/^php/i', ['PHP', 'js']); // ['PHP'] PHP Regex Functions Table
All function guides in this PHP tutorial series (11 tutorials in 4 categories).
Match
2 functionsTest for a single match or collect every occurrence and capturing groups.
| Function | Description | Tutorial |
|---|---|---|
preg_match() | Perform a regular expression match and optionally fill captures. | Try it |
preg_match_all() | Perform a global regular expression match. | Try it |
Replace & filter
4 functionsSubstitute text, keep only matched subjects, or compute each replacement with a callback.
| Function | Description | Tutorial |
|---|---|---|
preg_replace() | Perform a regular expression search and replace. | Try it |
preg_filter() | Perform a regular expression search and replace, returning only matches. | Try it |
preg_replace_callback() | Perform a regular expression search and replace using a callback. | Try it |
preg_replace_callback_array() | Perform a regular expression search and replace using callbacks per pattern. | Try it |
Split & grep
2 functionsBreak a string on a pattern, or keep array elements that match a pattern.
| Function | Description | Tutorial |
|---|---|---|
preg_split() | Split a string by a regular expression. | Try it |
preg_grep() | Return array entries that match a pattern. | Try it |
Quote & errors
3 functionsEscape literals for safe patterns, and read the last PCRE error code or message.
| Function | Description | Tutorial |
|---|---|---|
preg_quote() | Quote regular expression characters in a string. | Try it |
preg_last_error() | Return the error code of the last PCRE regex execution. | Try it |
preg_last_error_msg() | Return the error message of the last PCRE regex execution. | Try it |
Frequently Asked Questions
Start with preg_filter()
Open the next guide—syntax, examples, and FAQs.
7 people found this page helpful
