Making preg_match Case Insensitive
When using the preg_match function, case sensitivity can be a limiting factor in certain scenarios. Consider the following code:
preg_match("#(.{100}$keywords.{100})#", strip_tags($description), $matches);
This code attempts to display 100 characters on either side of the search string in the middle. However, it is case sensitive, potentially missing desired results.
Solution: Adding the 'i' Modifier
To make this code case insensitive, simply add the "i" modifier after the delimiter (# in this case):
preg_match("#(.{100}$keywords.{100})#i", strip_tags($description), $matches);
Alternatively, if the delimiter is "/" instead of "#", add the "i" after it:
preg_match("/your_regexp_here/i", $s, $matches);
With the "i" modifier, letters in the pattern will match both upper and lower case letters, ensuring that all relevant results are captured.
The above is the detailed content of How Do I Make `preg_match` Case-Insensitive?. For more information, please follow other related articles on the PHP Chinese website!