PHP preg_match() functionThe preg_match() function is a built-in function of PHP that performs a regular expression match. This function searches the string for pattern, and returns true if the pattern exists otherwise returns false. Generally, the searching starts from the beginning of $subject string parameter. The optional parameter $offset is used to start the search from the specified position. SyntaxNote: $offset is an optional parameter that specifies the position from where to begin the search.ParametersThis function accepts five parameters, which are described below: pattern It is a string type parameter. This parameter holds the pattern to search as a string. subject This parameter holds the input string in which we search for pattern. matches If matches parameter is provided, it will contain the search results. matches[0] - It will hold the text, which matched with the complete pattern. matches[1] - It will contain the text, which matched with the first captured parenthesized subpattern, and so on. flags The flags can have the following flags given below:
offset By default, the search starts from the beginning of the $subject parameter. The offset parameter is used to specify the place where the searching will start. It is an optional parameter. Return TypeThe preg_match() function returns true if pattern matches otherwise, it returns false. Note: If you only want to check whether one string is contained in another string, do not use preg_match() function. Use the strpos() function as it will be faster.ExamplesOutput: Array ( [0] => Array ( [0] => javatpoint [1] => 0 ) [1] => Array ( [0] => java [1] => 0 ) [2] => Array ( [0] => t [1] => 4 ) [3] => Array ( [0] => point [1] => 5 ) ) We can see the above output as given below to understand it better. Array ( [0] => Array ( [0] => javatpoint [1] => 0 ) [1] => Array ( [0] => java [1] => 0 ) [2] => Array ( [0] => t [1] => 4 ) [3] => Array ( [0] => point [1] => 5 ) ) Examples: case-insensitive search Output: Pattern matched in string. Array ( [0] => JTP ) Examples: by using word boundary (\b) Output: A match was found. A match was not found. Examples: get the domain name out from the URL Output: Domain name is: javatpoint.com Regex (Regular Expression) syntax
Explaining the pattern "[^[a-zA-Z0-9._-] +@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/]"
Next Topicpreg_replace() function |