Live Sandbox PHP preg_match() Function
PHP preg_match() Function
Instructions/Notes

preg_match($pattern, $subject, [, matches [, flags [, offset]]]) - This PHP function performs a regular expression match. This looks at the subject (your string) for a specified pattern. If a match is found, a 1 (true) is returned. Otherwise a 0 (false) is returned. See below for examples.





Some Examples
Pattern String Subject String preg_match Result
/An apple is red/ apple A match.
An apple is red/ apple Error. Missing forward slash (/) at the beginning of the pattern.
/An apple is red/ APPLE No match. preg_match is case sensitive.
/An apple is red/i APPLE A match. Add the letter "i" to the end of the pattern string to perform a case-insensitive match.
/Foo/i FOO A match.
/you s[a|4]ve/i you save A match.
/Luxury (?:shopping|timepiece)/i luxury No match.
/Luxury (?:shopping|timepiece)/i luxury timepieces A match.
/% OFF ALL (?:designer|luxury) /i Special 80% off all designer item A match.
/% OFF ALL (?:designer|luxury) /i 10% OFF all luxury watches A match.
/R[o|0]lex (?:w[a|4]tch|timepiece)/i r0lex watch A match.
/R[o|0]lex (?:w[a|4]tch|timepiece)/i Tag Heuer watch No match.
/R[o|0]lex (?:w[a|4]tch|timepiece)/i cheap rolex timepieces A match.
Suggestion

PHP Cookbook: Solutions & Examples

Amazon Reviewer Great help for someone thrown into the fray


Buy from Amazon