PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
The preg_match()
function in PHP is used to perform a pattern match on a string using regular expressions.
Here is a basic tutorial on how to use the preg_match()
function in PHP:
Syntax:
The syntax of preg_match()
is:
preg_match(string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] ) : int|false
$pattern
: The pattern to search for, as a string.$subject
: The input string.&$matches
: If provided, then it is filled with the results of search. $matches[0]
will contain the text that matched the full pattern, $matches[1]
will have the text that matched the first captured parenthesized subpattern, and so on.$flags
: It could be PREG_OFFSET_CAPTURE
. If this flag is passed, for every occurring match the appendant string offset will also be returned.$offset
: Normally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search.Return Value:
Returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.
Example:
Here is an example demonstrating how to use preg_match()
:
<?php $pattern = '/foo/'; $subject = "foobar"; // using preg_match() if (preg_match($pattern, $subject, $matches)) { echo "Match found: " . $matches[0]; } else { echo "No match found."; } ?>
In this example, the $pattern
is a regular expression that matches the string 'foo'. The preg_match()
function then checks if the pattern exists in the $subject
string and if it does, it outputs "Match found: foo". If the pattern does not exist in the string, it outputs "No match found.".
You can also find where the match starts in the string with the PREG_OFFSET_CAPTURE
flag:
<?php $pattern = '/foo/'; $subject = "foobar"; // using preg_match() with PREG_OFFSET_CAPTURE if (preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE)) { echo "Match found at position " . $matches[0][1]; } else { echo "No match found."; } ?>
In this example, the $pattern
is a regular expression that matches the string 'foo'. The preg_match()
function then checks if the pattern exists in the $subject
string and if it does, it outputs the starting position of the match in the string. If the pattern does not exist in the string, it outputs "No match found.".
Performing regular expression matching with preg_match()
in PHP:
preg_match()
is used to perform a regular expression match against a string.$pattern = "/apple/"; $text = "I have an apple."; if (preg_match($pattern, $text)) { echo "Match found!"; } else { echo "No match found."; }
Extracting and capturing groups with preg_match()
in PHP:
$pattern = "/(\d{2})-(\d{2})-(\d{4})/"; $dateString = "12-25-2022"; if (preg_match($pattern, $dateString, $matches)) { $day = $matches[1]; $month = $matches[2]; $year = $matches[3]; echo "Date: $month/$day/$year"; }
Case-insensitive matching in PHP preg_match()
:
i
flag for case-insensitive matching.$pattern = "/apple/i"; $text = "I have an Apple."; if (preg_match($pattern, $text)) { echo "Match found!"; } else { echo "No match found."; }
Returning the number of matches with preg_match()
in PHP:
$pattern = "/apple/"; $text = "I have an apple and another apple."; $numMatches = preg_match_all($pattern, $text, $matches); echo "Number of matches: $numMatches";
Using preg_match()
for pattern validation in PHP:
preg_match()
.$pattern = "/^[a-zA-Z0-9_]+$/"; $username = "user123"; if (preg_match($pattern, $username)) { echo "Valid username!"; } else { echo "Invalid username."; }
Handling multiple patterns with preg_match()
in PHP:
|
(pipe) symbol for OR conditions.$pattern = "/apple|orange/"; $text = "I have an orange."; if (preg_match($pattern, $text)) { echo "Match found!"; } else { echo "No match found."; }
Error handling and debugging with preg_match()
in PHP:
preg_last_error()
.$pattern = "/[a-z/"; $text = "Invalid pattern"; if (preg_match($pattern, $text)) { echo "Match found!"; } else { $error = preg_last_error(); echo "Error: $error"; }