PHP preg_match() Function

What is preg_match function?

If you want to know whether a string consists of a sub string that matches with a regular expression pattern or you want to know the sub string’s position in the string if it exists, then use preg_match() function. After finding the first match, this function stops further searching.

Syntax:

preg_match(pattern, subject, matches flags, offset)

Parameters:

pattern (Mandatory): This parameter is a regular expression that you’ll search in the “subject” parameter to find any match (Check Example 1).

subject (Mandatory): This is the main string where you’ll find match (Check Example 1).

matches (Optional): If any match found, this parameter stores search result as an array.

  • matches[0]: It contains string found in the “subject” parameter that matches the full pattern (Check example 2).
  • matches[1]: If there are groups in the regular expression, this element contains the first captured group’s (or first captured parenthesized pattern’s) result (Check example 2).
  • matches[2]: If there are groups in the regular expression, this element contains the second captured group’s (or second captured parenthesized pattern’s) result (Check example 2).
  • matches[3]: and, so on.

flags (Optional): you can use two values (PREG_OFFSET_CAPTURE and PREG_UNMATCHED_AS_NULL) as flags to return different values from the function-

  • PREG_OFFSET_CAPTURE: If you use this, the function returns each match as an array whether the “pattern” parameter has groups (Check example 3) or not (Check example 4). Each array has two elements – first element is the matching string found in the “subject” parameter and the second element is the matching position in the “subject” parameter.
  • PREG_UNMATCHED_AS_NULL: If you use this, the function returns NULL for each unmatched group used in the “pattern” parameter (Check example 5).

offset (Optional): Instead of starting search from the beginning of the “subject” parameter, if you want to start it from a specific position, mention by this “offset” parameter. (Check example 6).

Return Values:

  • It returns 1 if match found (Check example 7).
  • It returns 0 if no match found (Check example 8).
  • It returns FALSE if an error occurred.

Examples:

Example 1:

<pre>
<?php
$pattern = '/function/';
$str = "Learning PHP built-in functions make development easier.";
if (preg_match($pattern, $str)) {
    echo "Match found.";
}else{
    echo "Match not found.";
}
?>
</pre>

Output:

Match found.

Explanation:

Example 2:

<pre>
<?php
$pattern = '/(PHP) (built-in)/';
$str = "Learn PHP built-in functions.";
if (preg_match($pattern, $str, $matches)) {
    print_r($matches);
}
?>
</pre>

Output:

Array(
    [0] => PHP built-in
    [1] => PHP
    [2] => built-in
)

Explanation:

Example 3:

<pre>
<?php
$pattern = '/(PHP) (built-in)/';
$str = "Learn PHP built-in functions.";
if (preg_match($pattern, $str, $matches, PREG_OFFSET_CAPTURE)) {
    print_r($matches);
}
?>
</pre>

Output:

Array
(
    [0] => Array
        (
            [0] => PHP built-in
            [1] => 6
        )
     [1] => Array
        (
            [0] => PHP
            [1] => 6
        )
     [2] => Array
        (
            [0] => built-in
            [1] => 10
        ) 
)

Explanation:

Example 4:

<pre>
<?php
$pattern = '/PHP/';
$str = "Let's Learn PHP";
if (preg_match($pattern, $str, $matches, PREG_OFFSET_CAPTURE)) {
    print_r($matches);
}
?>

Output:

Array(
    [0] => Array
        (
            [0] => PHP
            [1] => 12
        ) 
)

Explanation:

Example 5:

<pre>
<?php
preg_match('/(built)*(function)/', 'PHP built-in function', $matches, PREG_UNMATCHED_AS_NULL);
var_dump($matches);
?>
</pre>

Output:

array(3) {
  [0]=>  string(8) “function”
  [1]=>  NULL
  [2]=>  string(8) “function”
}

Explanation:

Example 6:

<pre>
<?php
$pattern = '/learn/';
$str = "Do learn PHP built-in functions.";
if (preg_match($pattern, $str, $matches, PREG_OFFSET_CAPTURE, 4)) {
    print_r($matches);
}else{
    echo "Match not found";
}
?>

Output:

Match not found

Explanation:

Example 7:

<?php
$pattern = '/PHP/';
$str = "PHP built-in functions.";
$return_val = preg_match($pattern, $str);
    echo $return_val;
?>

Output:

1

Explanation:

Example 8:

<?php
$pattern = '/JavaScript/';
$str = "PHP built-in functions.";
$return_val = preg_match($pattern, $str);
    echo $return_val;
?>

Output:

0

Explanation:

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP preg_match() Function

preg_match() is a very powerful function as it uses regular expression pattern to find a sub string in a substring. You can also know the position of the substring in the string too. It is one of the Perl Compatible Regular Expressions (PCRE) functions in PHP.

Reference:

https://www.php.net/manual/en/function.preg-match.php