How to perform case insensitive str_contains?

How to perform case insensitive str_contains?

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

Stefan Bogdanescu

Founder & Senior Architect

Migrating from mb_stripos to Case-Insensitive str_contains

Replacing legacy logic in PHP often involves finding a native function that matches your custom implementation. You might have a helper like str_contains_old using mb_stripos to handle multi-byte characters while ignoring case. The goal is to migrate this to the new str_contains() function introduced in PHP 8.1. However, a direct replacement isn't possible because str_contains() is inherently case-sensitive. To achieve case-insensitive behavior with this new API, you must normalize the input strings before comparison.

The core issue lies in how mb_stripos handles Unicode normalization compared to standard string functions. When you call mb_stripos('Część', 'cZĘŚĆ'), PHP treats the characters as equivalent regardless of their specific case folding, returning true. In contrast, str_contains('Część', 'hello') returns false immediately because it performs a strict byte-by-byte or character-by-character comparison without altering the casing of either argument.

To replicate the behavior of your original helper function using str_contains(), you need to apply case folding to both the haystack and the needle. This is where PHP's built-in normalization functions become essential. You can use mb_strtolower() (or its ASCII equivalent strtolower() if you are certain your input contains only single-byte characters) to convert both strings to a uniform case before passing them to str_contains().

Here is how you would refactor the helper function:

function str_contains_ci(string $hay, string $needle): bool
{
    // Convert both strings to lowercase to ensure case-insensitive comparison
    return str_contains(strtolower($hay), strtolower($needle));
}

This approach ensures that 'Część' and 'cZĘŚĆ' are treated identically. When you pass the normalized strings into str_contains(), the function can perform its standard existence check against a uniform dataset, effectively mimicking the logic of mb_stripos without relying on it.

There is an important trade-off to consider regarding performance and character encoding. The strtolower() function is generally faster than mb_strtolower(), but it may not handle multi-byte characters or specific Unicode edge cases correctly if your application relies on complex locale settings. If your codebase frequently processes text in languages like Turkish, where the dotless 'I' and dotted 'I' have distinct collation rules, using strtolower() can lead to incorrect results. In those scenarios, you should stick with mb_strtolower() to preserve linguistic accuracy, even if it introduces a slight performance overhead.

Another pitfall is memory usage. Creating new string copies for normalization happens every time the function is called. If you are processing massive logs or streaming data, this allocation could add up. However, given that str_contains() itself is highly optimized in PHP 8.1+, the cost of normalization is usually negligible compared to the complexity reduction gained from dropping mb_stripos.

Finally, ensure your codebase is running PHP 8.1 or higher. While you can polyfill this logic for older versions, relying on native functions like str_contains() unlocks other performance optimizations available in newer runtimes. If you are using a framework like Laravel, remember that helper methods often sit atop these core language features, so updating your custom helpers aligns well with modernizing the underlying stack.

Our network