PHP preg_match() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Regex

Definition

In PHP, preg_match() tests whether a regular expression matches a string, and optionally fills $matches with the first match.

Patterns use delimiters, for example /pattern/flags. The function returns 1 on a match, 0 when there is no match, and false on error. It stops after the first match — use preg_match_all() for every match.

Syntax

Call preg_match() with the values you need. Start with this line.

PHP
$ok = preg_match('/\d+/', $text, $matches);
CallWhat it does
preg_match('/dog/', 'Cat and dog')Returns 1 because "dog" is found.
preg_match('/xyz/', 'hello')Returns 0 because there is no match.
preg_match('/(\d+)/', 'id 42', $m)Returns 1 and sets $m[0] to "42" and $m[1] to "42".

Always write patterns with delimiters: /.../ or #...# or ~...~. Add flags after the closing delimiter, such as /cat/i for case-insensitive matching.

Library declarations
PHP
preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int|false

Parameters

ParameterDescription
patternRequired. The PCRE pattern, including delimiters and optional flags (for example /\w+/i).
subjectRequired. The string to search.
matchesOptional. Passed by reference. Receives the full match at [0] and capturing groups at [1], [2], …
flagsOptional. For example PREG_OFFSET_CAPTURE to include byte offsets. Default 0.
offsetOptional. Byte offset where the search starts. Default 0.

Return Value

Return ValueDescription
int|false1 if the pattern matches, 0 if it does not, or false if an error occurred.

PHP Examples

Example 1 — First Match with Delimiters

Use a simple /pattern/ and read the return value plus $matches.

PHP
<?php
$subject = 'Cat and dog';
$result = preg_match('/dog/', $subject, $matches);

echo $result, PHP_EOL;
print_r($matches);

How It Works

  1. The pattern is written as /dog/ — slashes are the delimiters around the text to find.
  2. preg_match() searches $subject and stops at the first match.
  3. The return value 1 means a match was found.
  4. $matches[0] holds the full matched text dog.

Example 2 — No Match Versus Capturing Groups

See a zero result, then a match with parentheses that capture parts.

PHP
<?php
var_dump(preg_match('/xyz/', 'hello'));

preg_match('/(\\d+)-(\\d+)/', 'Room 12-34', $m);
echo $m[0], PHP_EOL;
echo $m[1], PHP_EOL;
echo $m[2], PHP_EOL;

How It Works

  1. xyz is not in hello, so preg_match() returns 0 (no match).
  2. The second pattern uses parentheses to capture groups.
  3. $m[0] is the full match 12-34.
  4. $m[1] and $m[2] are the two captured numbers.

Example 3 — Case-Insensitive Flag

Put a flag after the closing delimiter to change matching behavior.

PHP
<?php
echo preg_match('/php/', 'I like PHP'), PHP_EOL;
echo preg_match('/php/i', 'I like PHP'), PHP_EOL;

How It Works

  1. Without a flag, /php/ is case sensitive, so PHP does not match.
  2. Adding i after the delimiter makes the search case insensitive.
  3. /php/i matches PHP and the call returns 1.
  4. Flags always go after the closing delimiter, not inside the pattern text.

Frequently Asked Questions

preg_match() checks whether a regular expression matches a string. It returns 1 on success, 0 on no match, and false on error. It can fill $matches with the first match.
PCRE patterns in PHP must start and end with a delimiter character. The same character marks the end of the pattern; flags such as i or m come after that closing delimiter.
preg_match() finds only the first match. preg_match_all() finds every match and fills $matches with all of them.
Call preg_quote($user, '/') so special characters are escaped, then place the result inside your delimiters. Never concatenate raw user input into a pattern.
An error occurred (bad pattern, limits, and so on). That is different from 0, which means “no match”. Check preg_last_error() or preg_last_error_msg().

Next: preg_match_all()

preg_match_all() finds every match of a regular expression in a string and stores them in .

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

8 people found this page helpful