How do I make this preg_match case insensitive?

How do I make this preg_match case insensitive?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Making preg_match Case Insensitive in Laravel

The regex pattern you are using relies on standard PCRE syntax, and adding case-insensitivity is straightforward once you understand where the flag sits relative to your quantifiers. Your current code captures 100 characters before and after a specific keyword:

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

To make this case-insensitive, you need to add the i modifier. In PHP, regex modifiers are appended immediately after the closing delimiter of your pattern string. Assuming you are using the standard /.../ or #...# delimiters, the syntax changes slightly depending on which one you prefer, but the placement remains consistent:

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

Notice how the i follows the closing #. This tells the PCRE engine to ignore character case when matching. If you were using slashes instead of hashes, it would look like this:

preg_match('/(.{100}$keywords.{100})/i', strip_tags($description), $matches);

The Role of strip_tags

Your code includes strip_tags() before passing the string to preg_match. This is a critical detail because HTML tags often contain uppercase letters (like <SCRIPT> or <DIV>) that could interfere with your matching logic. If you are searching inside an HTML block without stripping tags, your case-insensitive flag might match text inside a tag attribute or fail entirely if the tag itself disrupts the character count.

By stripping tags first, you ensure that $keywords matches actual content rather than markup artifacts. However, be aware that strip_tags() does not remove comments. If your keyword appears inside an HTML comment like <!-- The Keyword -->, it will still be present in the string after strip_tags(). Depending on your security requirements or content validation rules, you might need to use a more robust parser if comments are a concern in your specific use case.

Performance Considerations with Large Strings

Your pattern uses {100} quantifiers twice, meaning it attempts to match 200 characters plus the keyword itself. While this is efficient for small snippets, performance can degrade significantly on very large strings. The engine must backtrack if the first 100 characters don't align correctly before finding a valid start point.

If you are processing massive text bodies where performance becomes a bottleneck, consider using preg_match_all instead of preg_match. This allows you to iterate over all occurrences without manually looping through matches later. However, keep in mind that matching 200+ characters might not be necessary if your goal is simply to find the keyword regardless of surrounding whitespace or text length. If strict adjacency isn't required, relaxing the quantifiers could improve speed.

Troubleshooting Common Pitfalls

A frequent mistake developers make when adding the i flag is assuming it applies to the entire string globally rather than just the pattern match. The i modifier only affects how the regex engine evaluates the characters within the pattern itself. It does not change how PHP handles the input string variables.

Another potential issue arises if $keywords contains special regex characters like dots (.), asterisks (*), or parentheses ((). Even with the i flag, these characters have special meanings in PCRE. For example, if your keyword is "test.", the dot will match any character due to its default behavior, not just a literal period. You must escape literal characters using preg_quote($keywords, '/') before injecting them into your pattern. Without escaping, your case-insensitive logic might return unexpected matches because the regex engine interprets parts of the keyword as wildcards.

For more complex text processing needs in Laravel, you might explore packages like spatie/laravel-text-to-speech or similar utility libraries that handle fuzzy matching and stemming if exact character counts become a constraint. However, for strict substring extraction, sticking to native preg_match with proper modifiers and escaping is usually the most performant approach.

Our network