Home >Backend Development >PHP Tutorial >How Do I Make `preg_match` Case-Insensitive?

How Do I Make `preg_match` Case-Insensitive?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-02 20:10:13122browse

How Do I Make `preg_match` Case-Insensitive?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn