Online Tutorials & Training Materials | STechies.com
Register Login

PHP preg_match() Function

13 Nov 2019 1:17 pm || 0

The preg_match() function is used to match strings with regular expressions. This function searches for a pattern in a string and returns true if the pattern exists otherwise false.

Note: The search for pattern by-default starts from the beginning of subject string but the user can an optional parameter offset to specify the position to start the search

What is Regular expression?

A regular expression is a sequence of a special text string that describes search patterns. It uses arithmetic operators such as (+,-,^) to create complex expression.

Syntax 

int preg_match( $pattern, $input, $matches, $flags, $offset )

Parameters

The preg_match function takes five parameters. 

Parameter Type Description
$pattern   Specifies the pattern which is needed to be searched as a string
$input   Specifies the input string
$matches  

if the matches parameter is specified, then it will be filled with the results of the 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  

$flag parameter can be a combination of the following flags:

  • PREG_OFFSET_CAPTURE
  • PREG_UNMATCHED_AS_NULL
     
$offset   This parameter specifies the position to start the search

Return Value

This function preg_match() returns 1 if the pattern matches given string subject otherwise return 0, or FALSE if any error occurred.

Note: In incase the offset is higher than string subject length, the function will return false.

Examples of PHP preg_match() function

Program 1: Simple Program

<?php

// Declare a variable and initialize it
$string = 'STechies Free Training  Tutorials for Techie';

// Use preg_match() function to check match
$result = preg_match('/(STechies)/', $string);

if($result == 1){
    echo "Matched";
}else{
    echo "Do not match";
}

?>

Output:

Matched 

Program 2: Program to print the value of the match array

// variable Declaration and Initialization
$string = 'STechies Free Training  Tutorials for Techie';

// Use preg_match() function to check match
preg_match('/(STechies)/', $string, $matches);

print_r($matches);
?>

OUTPUT

Array ( [0] => STechies [1] => STechies )

Program 3: Program to illustrate flags in the preg_match Function

<?php
//Program to illustrate flags in the preg_match Function
//With `PREG_OFFSET_CAPTURE` flag

// variable Declaration and Initialization
$string = 'STechies Free Training Tutorials for Techie';

// preg_match() function to check match the string
preg_match('/(STechies)/', $string, $matches, PREG_OFFSET_CAPTURE);

print_r($matches);
?>

OUTPUT

Array ( [0] => Array ( [0] => STechies [1] => 0 ) [1] => Array ( [0] => STechies [1] => 0 ) )

Program 4: Program to illustrate matches in the preg_match function

<?php
//Program to illustrate matches in the preg_match Function
//With `PREG_UNMATCHED_AS_NULL` flag

preg_match('/(a)(r)*(c)/', 'acrtuv', $matches);
var_dump($matches);
preg_match('/(a)(r)*(c)/', 'acrtuv', $matches, PREG_UNMATCHED_AS_NULL);
var_dump($matches);
?>

OUTPUT

array(4) { [0]=> string(2) "ac" [1]=> string(1) "a" [2]=> string(0) "" [3]=> string(1) "c" }
array(4) { [0]=> string(2) "ac" [1]=> string(1) "a" [2]=> string(0) "" [3]=> string(1) "c" }

Program 4: Php program to illustrate preg_match (Case In-sensitive)

<?php

//Php program to illustrate preg_match (Case In-sensitive)

//Moderate Program

/* The \b in the pattern indicates a word boundary, so only the distinct
 * word "stechies" is matched,*/
if (preg_match("/\bstechies\b/i", "STechies - Free Training Tutorials for Techie")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
?>

OUTPUT:

A match was found.