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

How Do I Make `preg_match` Case-Insensitive?

Mary-Kate Olsen
Release: 2024-12-02 20:10:13
Original
119 people have browsed it

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);
Copy after login

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);
Copy after login

Alternatively, if the delimiter is "/" instead of "#", add the "i" after it:

preg_match("/your_regexp_here/i", $s, $matches);
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template