"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How Do I Make a `preg_match` Regular Expression Case-Insensitive?

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

Published on 2025-01-01
Browse:747

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.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3