PHP Regex Functions

Beginner
5 min read
Updated: Nov 2026
11 function guides
Function index

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().

Beginner path.

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.

Eleven guides.

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:

PHP
$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 functions

Test for a single match or collect every occurrence and capturing groups.

FunctionDescriptionTutorial
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 functions

Substitute text, keep only matched subjects, or compute each replacement with a callback.

FunctionDescriptionTutorial
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 functions

Break a string on a pattern, or keep array elements that match a pattern.

FunctionDescriptionTutorial
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 functions

Escape literals for safe patterns, and read the last PCRE error code or message.

FunctionDescriptionTutorial
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

They are the built-in preg_* functions for PCRE: match with preg_match() and preg_match_all(), replace with preg_replace(), preg_filter(), and the callback helpers, split and filter arrays with preg_split() and preg_grep(), escape with preg_quote(), and inspect errors with preg_last_error() and preg_last_error_msg(). This index links to a full tutorial for each one.
The Regex page teaches the ideas: delimiters, preg_match() basics, preg_match_all(), preg_replace(), preg_quote(), and when to use callbacks, with three fixed offline examples. This index lists every function tutorial so you can open preg_filter(), preg_match(), preg_replace(), and the rest.
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. The sidebar begins with preg_filter() because of alphabetical order.
Both apply a pattern and replacement. preg_replace() returns the (possibly unchanged) subject. preg_filter() returns only subjects that matched—null for a non-matching string, or only matching items when the subject is an array.
Use a callback when each replacement must be computed in PHP—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.
Open preg_filter() next (it is the first guide in sidebar order), or jump to preg_match() and preg_replace() if you want the everyday match and replace patterns first. Read PHP Regex if delimiters and groups still feel unfamiliar.

Start with preg_filter()

Open the next guide—syntax, examples, and FAQs.

preg_filter() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful