PHP preg_match() function

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.

Syntax

snippet
int preg_match (string $pattern, string $subject, array $matches, int $flags, int $offset)
Note
Note: $offset is an optional parameter that specifies the position from where to begin the search.

Parameters

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:

  • PREG_OFFSET_CAPTURE: If this flag is passed in preg_match(), for every occurring match the appendant string offset will also return.
  • PREG_UNMATCHED_AS_NULL: If this flag is passed in preg_match(), unmatched subpattern will be reported as NULL, otherwise they will be reported as empty string.

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.

Return Type

The preg_match() function returns true if pattern matches otherwise, it returns false.

Note
Note: If you only want to check whether one string is contained in another string, do not use preg_match() function. Use the strpos() function as it will be faster.

Examples

snippet
<?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:

Output
Array ( [0] => Array ( [0] => rookienerd [1] => 0 ) [1] => Array ( [0] => java [1] => 0 ) [2] => Array ( [0] => t [1] => 4 ) [3] => Array ( [0] => point [1] => 5 ) )

We can see the above output as given below to understand it better.

Output
Array ( [0] => Array ( [0] => rookienerd [1] => 0 ) [1] => Array ( [0] => java [1] => 0 ) [2] => Array ( [0] => t [1] => 4 ) [3] => Array ( [0] => point [1] => 5 ) )

Examples: case-insensitive search

snippet
<?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:

Output
Pattern matched in string. Array ( [0] => JTP )

Examples: by using word boundary (\b)

snippet
<?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:

Output
A match was found. A match was not found.

Examples: get the domain name out from the URL

snippet
<?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:

Output
Domain name is: rookienerd.com

Regex (Regular Expression) syntax

[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

Explaining the pattern "[^[a-zA-Z0-9._-] +@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/]"

  • ""/?/"" It shows start and end of regular expression.
  • "[^[a-zA-Z0-9._-]" It matches any uppercase or lowercase letters, numbers between 0 to 9, dot, underscore, or dashes.
  • "+@[a-zA-Z0-9-]" It matches the @ symbol followed by the upper or lowercase letters, numbers between 0 and 9 or dashes.
  • "+\.[a-zA-Z.]{2,5}$/" The dot is escaped by using backslash and then matches any lower or uppercase letters with a length between 2 and 5 at the end of string.
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +