Reputation: 1475
How can I make this regex case in-sensitive?
preg_match('/[\w\d\.]+\.(com|org|ca|net|uk|co|it)/', $string, $matches)
Upvotes: 1
Views: 1219
Reputation: 41838
A. A Modifier After the Pattern Applies to the Whole Pattern
Example:
$match_or_not = preg_match("~somepattern~i",$subject);
The PHP Manual says this:
i (PCRE_CASELESS) If this modifier is set, letters in the pattern match both upper and lower case letters.
B. An Inline Modifier Can Turn Case-Insensitivity On and Off
You can apply it to the whole string like so—but you don't have to:
$match_or_not = preg_match("~(?i)somepattern~",$subject);
You can turn it off like so:
$match_or_not = preg_match("~(?i)some (?-i)pattern~",$subject);
To avoid turning it back on, you can turn it off temporarily within parentheses:
$match_or_not = preg_match("~(?i)some (?-i:other) pattern~",$subject);
From the PCRE doc:
The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and PCRE_EXTENDED options (which are Perl-compatible) can be changed from within the pattern by a sequence of Perl option letters enclosed between "(?" and ")". The option letters are
i for PCRE_CASELESS m for PCRE_MULTILINE s for PCRE_DOTALL
x for PCRE_EXTENDEDFor example, (?im) sets caseless, multiline matching. It is also possible to unset these options by preceding the letter with a hyphen, and a combined setting and unsetting such as (?im-sx), which sets PCRE_CASELESS and PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED, is also permitted. If a letter appears both before and after the hyphen, the option is unset.
The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA can be changed in the same way as the Perl-compatible options by using the characters J, U and X respectively.
When one of these option changes occurs at top level (that is, not inside subpattern parentheses), the change applies to the remainder of the pattern that follows. If the change is placed right at the start of a pattern, PCRE extracts it into the global options (and it will therefore show up in data extracted by the pcre_fullinfo() function).
An option change within a subpattern (see below for a description of subpatterns) affects only that part of the subpattern that follows it, so
(a(?i)b)c
matches abc and aBc and no other strings (assuming PCRE_CASELESS is not used). By this means, options can be made to have different settings in different parts of the pattern. Any changes made in one alternative do carry on into subsequent branches within the same subpattern. For example,
(a(?i)b|c)
matches "ab", "aB", "c", and "C", even though when matching "C" the first branch is abandoned before the option setting. This is because the effects of option settings happen at compile time. There would be some very weird behaviour otherwise.
Upvotes: 1
Reputation: 11233
Possibly, adding i
at the end:
preg_match('/[\w\d\.]+\.(com|org|ca|net|uk|co|it)/i', $string, $matches)
Upvotes: 1
Reputation: 174706
Add case-insensitive modifier (?i)
to the start of the pattern,
(?i)[\w\d\.]+\.(com|org|ca|net|uk|co|it)
You code will looks like,
preg_match('/(?i)[\w\d\.]+\.(com|org|ca|net|uk|co|it)/', $string, $matches)
Upvotes: 1