The preg_match() function is a built-in function of PHP that performs a regular expression match. This function searches the string for pattern, and returns true if the pattern exists otherwise returns false.
Generally, the searching starts from the beginning of $subject string parameter. The optional parameter $offset is used to start the search from the specified position.
int preg_match (string $pattern, string $subject, array $matches, int $flags, int $offset)
This function accepts five parameters, which are described below:
pattern
It is a string type parameter. This parameter holds the pattern to search as a string.
subject
This parameter holds the input string in which we search for pattern.
matches
If matches parameter is provided, it will contain the search results.
matches[0] - It will hold the text, which matched with the complete pattern.
matches[1] - It will contain the text, which matched with the first captured parenthesized subpattern, and so on.
flags
The flags can have the following flags given below:
offset
By default, the search starts from the beginning of the $subject parameter. The offset parameter is used to specify the place where the searching will start. It is an optional parameter.
The preg_match() function returns true if pattern matches otherwise, it returns false.
<?php //initialize a variable of string type $site = "rookienerd"; preg_match('/(java)(t)(point)/', $site, $matches, PREG_OFFSET_CAPTURE); //display the matches result print_r($matches); ?>
Output:
We can see the above output as given below to understand it better.
Examples: case-insensitive search
<?php //initialize a variable of string type $website = "JTP is a best online platform to learn."; //case insensitive search for word jtp //The "i" after pattern delimiter indicates case-insensitive search $res = preg_match('/jtp/i', $website, $matches); if ($res) { echo "Pattern matched in string.</br>"; print_r($matches); } else { echo "Pattern not matched in string."; } ?>
Output:
Examples: by using word boundary (\b)
<?php /* The \b indicates the word boundary in the pattern. So, it matches only the distinct word like "web", and words like "coreweb" or " webinar" do not match partially.*/ if (preg_match("/\bweb\b/i", "PHP is a web scripting language.")) { echo "A match was found. </br>"; } else { echo "A match was not found. </br>"; } if (preg_match("/\bweb\b/i", "PHP is a website scripting language.")) { echo "A match was found."; } else { echo "A match was not found."; } ?>
Output:
Examples: get the domain name out from the URL
<?php // get host name from URL preg_match('@^(?:https://)?([^/]+)@i', "https://www.rookienerd.com/php-tutorial", $matches); $host = $matches[1]; // get last two segments of host name preg_match('/[^.]+\.[^.]+$/', $host, $matches); echo "Domain name is: {$matches[0]}\n"; ?>
Output:
[abc] | Matches a single character - a, b, or c |
[^abc] | Matches any single character but a, b, or c |
[a-z] | Matches any single character within the range a-z |
[a-zA-Z] | Any single character within the range a-z or A-Z |
^ | Start of line |
$ | End of line |
\A | Start of string |
\z | End of string |
. | Any single character |
\s | Any whitespace character |
\S | Any non-whitespace character |
\d | Any digit |
\D | Any non-digit |
\w | Any word character (letter, number, underscore) |
\W | Any non-word character |
\b | Word boundary checker |
/?/ | Starts and ends the regular expression |
(?) | Capture everything enclosed in parenthesis () |
(a|b) | a or b |
a? | Zero or one of a |
a* | Zero or more of a |
a+ | One or more of a |
a{3} | Exactly 3 of a |
a{3,} | 3 or more of a |
a{3,6} | Between 3 and 6 of a |
i | Case insensitive check |
m | Make dot match newlines |
x | Ignore whitespace in regex |