PHP preg_match() Function

PHP preg_match_all() Function
PHP preg_grep() Function

In this article, you will learn how to match a pattern in the PHP string. The preg_match() function in PHP returns whether a match was found in a string.

what is the syntax of the PREG_MATCH function in php?

preg_match(pattern, input, matches, flags, offset)
ParameterDescription
patternRequired. Contains a regular expression indicating what to search for
inputRequired. The string in which the search will be performed
matchesOptional. The variable used in this parameter will be populated with an array containing all of the matches that were found
flagsOptional. A set of options that change how the matches array is structured:PREG_OFFSET_CAPTURE – When this option is enabled, each match, instead of being a string, will be an array where the first element is a substring containing the match and the second element is the position of the first character of the substring in the input.PREG_UNMATCHED_AS_NULL – When this option is enabled, unmatched subpatterns will be returned as NULL instead of as an empty string.
offsetOptional. Defaults to 0. Indicates how far into the string to begin searching. The preg_match() function will not find matches that occur before the position given in this parameter
PHP PREG_MATCH() method

examples of the PREG_MATCH function

Example 1. In this example, we use a regular expression to do a case-insensitive search for “w3schools” in a string.

<?php
$str = "Visit W3Schools";
$pattern = "/w3schools/i";
echo preg_match($pattern, $str);
?>
PHP preg_match_all() Function
PHP preg_grep() Function

Stay up-to-date about PHP!

We don’t spam!

en English
X
Scroll to Top