preg_match function searches strings for patterns and it returns true when pattern is found and returns false if it doesn’t. Typically, search begins from the starting point of subject string. The parameter offset can be used to define the exact position from which to begin the search.
int preg_match( $pattern, $input, $matches, $flags, $offset )
The function takes 5 parameters. They are listed in the following paragraphs:
It’s a string-type parameter. This parameter is used to store the pattern to search in an unicode string.
This parameter contains the input string, which we look for patterns.
If the parameter matches is set it will be a list of results of the search.
matches[0match[0 the pattern, which matches to the entire pattern. matches[1match[1 the textthat matches the first subpattern that was parenthesized that matches the first subpattern, and so on.
PREG_OFFSET_CAPTURE When this flag has been specified in the preg_match() for each occuring match, the appropriate offset of the string will also be returned.
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.
In default, the search begins from the start of $subject. The offset parameter can be used to determine the location where the search will begin. It’s an option.
Preg_match() function will return true only if the the pattern matches and if not, it returns false.
/Declare a variable, and then initialize it.
DAD = $ “DAD is the best Platform. “;
Case-insensitive search to find the term “DAD”
if (preg_match(“/\bDAD\b/i”, $gfg, $match))
echo “Matched! “;
Other than that
Echo “not matched”;
Know more about the Preg_match() function
- A Regex or Regular Expression within PHPcan be described as a pattern matching algorithm
- Regular expressions are extremely useful in validation checks, generating HTML templates that can recognize tags and other tags.
- PHP includes built-in functions like PHP preg_match(), PHP preg_split() and PHP preg_replace() which support regular expressions.
- Metacharacters let us make complex patterns