preg_match() / preg_match_all() Since: PHP 4(2000)
Performs pattern matching on a string using regular expressions. You can check whether a string matches a specific pattern and extract the matched portions.
Syntax
// Gets the first match of the pattern. preg_match($pattern, $subject, $matches, $flags, $offset); // Gets all matches of the pattern. preg_match_all($pattern, $subject, $matches, $flags, $offset); // Escapes special regex metacharacters in a string. preg_quote($str, $delimiter);
Function List
| Function | Description |
|---|---|
| preg_match($pattern, $subject, &$matches) | Checks whether the regex pattern matches the string. Returns only the first match, and stores the result in the array passed as the third argument. |
| preg_match_all($pattern, $subject, &$matches) | Finds all matches of the regex pattern in the string. Returns the number of matches found. |
| preg_quote($str, $delimiter) | Returns a string with all special regex characters escaped. Use this when embedding user input into a regex pattern. |
Return Value
preg_match() returns 1 if a match is found, 0 if no match is found, or false on error. preg_match_all() returns the number of matches. preg_quote() returns the escaped string.
Sample Code
<?php
// Basic pattern matching.
if (preg_match("/php/i", "PHP is great")) {
echo "Matched."; // Matches regardless of case.
}
// Extract the matched portion.
preg_match("/(\d{4})-(\d{2})-(\d{2})/", "Registered: 2025-04-15", $matches);
echo $matches[0]; // Full match — outputs '2025-04-15'.
echo $matches[1]; // First capture group — outputs '2025'.
echo $matches[2]; // Second capture group — outputs '04'.
echo $matches[3]; // Third capture group — outputs '15'.
// Use named capture groups.
$pattern = "/(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})/";
preg_match($pattern, "2025-04-15", $matches);
echo $matches["year"]; // Outputs '2025'.
echo $matches["month"]; // Outputs '04'.
// Validate an email address format.
$email = "user@example.com";
if (preg_match("/^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$/", $email)) {
echo "Valid email address.";
}
// Match a string containing multibyte characters. The u flag enables UTF-8 mode.
preg_match("/[ぁ-ん]+/u", "Hello こんにちは World", $matches);
echo $matches[0]; // Outputs 'こんにちは'.
// Use preg_match_all() to find all matches.
$html = '<img src="photo1.jpg"><img src="photo2.png"><img src="photo3.gif">';
$count = preg_match_all('/src="([^"]+)"/', $html, $matches);
echo $count . " matches found."; // Outputs '3 matches found.'.
print_r($matches[1]); // Outputs '["photo1.jpg", "photo2.png", "photo3.gif"]'.
// Extract all phone numbers from a string.
$text = "Contact us at 03-1234-5678 or 090-9876-5432.";
preg_match_all("/\d{2,4}-\d{2,4}-\d{4}/", $text, $matches);
foreach ($matches[0] as $phone) {
echo $phone . "\n"; // Outputs each matched phone number.
}
// Escape user input with preg_quote().
$user_input = "100$ (tax included)";
$escaped = preg_quote($user_input, "/");
echo $escaped; // Outputs '100\$ \(tax included\)'. Special characters are escaped.
// Safely embed the escaped string in a pattern.
$search = "C++";
$pattern = "/" . preg_quote($search, "/") . "/";
if (preg_match($pattern, "Languages: C++, Java, PHP")) {
echo "'" . $search . "' was found.";
}
Notes
PHP's regex functions are based on the PCRE (Perl Compatible Regular Expressions) library. Patterns are specified in the format /pattern/modifiers. Common modifiers include i (case-insensitive), m (multiline mode), and u (UTF-8 mode).
preg_match() returns only the first match, while preg_match_all() returns all matches. If you only need to check whether a match exists, you can use the return value of preg_match() directly as an if condition. The third argument $matches can be omitted if you do not need the matched results.
When processing strings that contain multibyte characters such as Japanese, add the u modifier at the end of your pattern to enable UTF-8 mode. Without it, multibyte characters will not be processed correctly. When including user input in a regex pattern, always escape it with preg_quote() to prevent injection attacks. For regex-based replacement and splitting, see preg_replace() / preg_split().
If you find any errors or copyright issues, please contact us.