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)
Parameter | Description |
---|---|
pattern | Required. Contains a regular expression indicating what to search for |
input | Required. The string in which the search will be performed |
matches | Optional. The variable used in this parameter will be populated with an array containing all of the matches that were found |
flags | Optional. 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. |
offset | Optional. 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 |
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);
?>