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);
Call
What 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
Parameter
Description
pattern
Required. The PCRE pattern, including delimiters and optional flags (for example /\w+/i).
subject
Required. The string to search.
matches
Optional. Passed by reference. Receives the full match at [0] and capturing groups at [1], [2], …
flags
Optional. For example PREG_OFFSET_CAPTURE to include byte offsets. Default 0.
offset
Optional. Byte offset where the search starts. Default 0.
Return Value
Return Value
Description
int|false
1 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.
xyz is not in hello, so preg_match() returns 0 (no match).
The second pattern uses parentheses to capture groups.
$m[0] is the full match 12-34.
$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;
Output
0
1
How It Works
Without a flag, /php/ is case sensitive, so PHP does not match.
Adding i after the delimiter makes the search case insensitive.
/php/i matches PHP and the call returns 1.
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().