PHP Modules Tutorials - Herong's Tutorial Examples - v5.18, by Herong Yang
preg_match() - Regular Expression Match
This section provides a tutorial example on how to search for the first occurrence and all occurrences with a given regular expression pattern using preg_match() and preg_match_all() functions.
The most commonly used regular expression operation is the pattern match operation. PCRE module provides 2 pattern match functions:
$found = preg_match($pattern, $subject, $matches, $flags);
$found = preg_match_all($pattern, $subject, $matches, $flags);
where:
$pattern: The regular express pattern to search for
$subject: The string to search against
$matches: Array to store matched substrings
$flags: Optional - Binary flags to turn various options
PREG_OFFSET_CAPTURE: Flag to capture offsets of matches
PREG_SET_ORDER: Flag to order matches by occurrence
$found: Boolean true if match found
Here is an example script that searches regular expression patterns for the first occurrence and all occurrences.
<?php
# preg-match-with-sub-pattern.php
# Copyright 2009-2024 (c) HerongYang.com. All Rights Reserved.
$subject = "herongyang.com/PHP, herongyang.com/Python";
$pattern = "/[^\/]+\/([^,]+)/";
print("Subject: $subject\n");
print("Pattern: $pattern\n");
print("Match for the first occurrence:\n");
$flags = 0;
$found = preg_match($pattern, $subject, $matches, $flags);
if ($found) {
print(" First match: {$matches[0]}\n");
print(" First sub-match: {$matches[1]}\n");
} else {
print(" No match found\n");
}
print("Match for the first occurrence and return offsets:\n");
$flags = PREG_OFFSET_CAPTURE;
$found = preg_match($pattern, $subject, $matches, $flags);
if ($found) {
print(" First match: {$matches[0][0]} at {$matches[0][1]}\n");
print(" First sub-match: {$matches[1][0]} at {$matches[1][1]}\n");
} else {
print(" No match found\n");
}
print("Match for all occurrences:\n");
$flags = PREG_SET_ORDER;
$found = preg_match_all($pattern, $subject, $matches, $flags);
if ($found) {
for ($i=0; $i<count($matches); $i++) {
print(" Match #".($i+1).":\n");
print(" Full match: {$matches[$i][0]}\n");
print(" Sub-match: {$matches[$i][1]}\n");
}
} else {
print(" No match found\n");
}
?>
If you run the above script, you will see the following output:
herong$ preg-match-with-sub-pattern.php
Subject: herongyang.com/PHP, herongyang.com/Python
Pattern: /[^\/]+\/([^,]+)/
Match for the first occurrence:
First match: herongyang.com/PHP
First sub-match: PHP
Match for the first occurrence and return offsets:
First match: herongyang.com/PHP at 0
First sub-match: PHP at 15
Match for all occurrences:
Match #1:
Full match: herongyang.com/PHP
Sub-match: PHP
Match #2:
Full match: , herongyang.com/Python
Sub-match: Python
Table of Contents
Introduction and Installation of PHP
Managing PHP Engine and Modules on macOS
Managing PHP Engine and Modules on CentOS
DOM Module - Parsing HTML Documents
GD Module - Manipulating Images and Pictures
MySQLi Module - Accessing MySQL Server
OpenSSL Module - Cryptography and SSL/TLS Toolkit
►PCRE Module - Perl Compatible Regular Expressions
►preg_match() - Regular Expression Match
preg_grep() - Search Array Members
preg_replace() - Replace Matched Substring
preg_split() - Split String into Tokens
preg_quote() - Escape Characters in Pattern
SOAP Module - Creating and Calling Web Services
SOAP Module - Server Functions and Examples