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

How Do I Make a `preg_match` Regular Expression Case-Insensitive?

Linda Hamilton
Linda HamiltonOriginal
2024-11-29 18:43:15501browse

How Do I Make a `preg_match` Regular Expression Case-Insensitive?

Making preg_match Case Insensitive

In the code snippet provided in the question, case sensitivity is preventing the intended result from being achieved. To rectify this, you can use the i modifier in your regular expression, ensuring it becomes case-insensitive.

Here's how you can modify the code:

preg_match("#(.{100}$keywords.{100})#i", strip_tags($description), $matches);

By adding the i modifier after the delimiter (# in this case), the regular expression will become case-insensitive. This means that it will match both uppercase and lowercase letters, resolving the problem of case sensitivity in the original code.

The i modifier can be used with any delimiter, not just #. If you are using the / delimiter, you would add the i modifier after it like this:

preg_match("/your_regexp_here/i", $s, $matches); // i means case insensitive

When the i modifier is set, the letters in the pattern will match both upper and lower case letters. This allows the code to work correctly regardless of the case of the characters in the input string.

The above is the detailed content of How Do I Make a `preg_match` Regular Expression 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