Case insensitive string comparison

Deniz Zoeteman picture Deniz Zoeteman · Mar 29, 2011 · Viewed 82.5k times · Source

I would like to compare two variables to see if they are the same, but I want this comparison to be case-insensitive.

For example, this would be case sensitive:

if($var1 == $var2){
   ...
}

But I want this to be case insensitive, how would I approach this?

Answer

asthasr picture asthasr · Mar 29, 2011

This is fairly simple; you just need to call strtolower() on both variables.

If you need to deal with Unicode or international character sets, you can use mb_strtolower().

Please note that other answers suggest using strcasecmp()—that function does not handle multibyte characters, so results for any UTF-8 string will be bogus.