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

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

Output:

Array ( [0] => Array ( [0] => javatpoint [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.

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

Examples: case-insensitive search

Output:

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

Examples: by using word boundary (\b)

Output:

A match was found.
A match was not found.

Examples: get the domain name out from the URL

Output:

Domain name is: javatpoint.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
\AStart of string
\zEnd of string
.Any single character
\sAny whitespace character
\SAny non-whitespace character
\dAny digit
\DAny non-digit
\wAny word character (letter, number, underscore)
\WAny non-word character
\bWord 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
iCase insensitive check
mMake dot match newlines
xIgnore 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.





Latest Courses