Regular expression pattern matching function for the PHP programming language
The preg_match()
function in php searches for a match to the regular expression given using Perl Compatible Regular Expression (PCRE
) syntax. Specifically, it is for matching one value or one set of data with a given pattern.
If you require multiple matches that match your pattern, you will want to use the preg-match-all function
Description
int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
Basic matching
preg_match('/PHP/', 'PHP') # Match for an unbound literal
preg_match('/^PHP/', 'PHP') # Match literal at start of string
preg_match('/PHP$/', 'PHP') # Match literal at end of string
preg_match('/^PHP$/', 'PHP') # Match for exact string content
preg_match('/^$/', '') # Match empty string
Using different regular expression delimiters
preg_match('/PHP/', 'PHP') # / as commonly used delimiter
preg_match('@PHP@', 'PHP') # @ as delimiter
preg_match('!PHP!', 'PHP') # ! as delimiter
References