Jamol
Jamol

Reputation: 3918

php case-insensitive comparison of russian characters

How can I compare russian characters case insensitively. I have tried:

if(strcasecmp($content->title, 'О нас') == 0){
        $about=$content->title;
    }

and also

if(strtolower($content->title) == strtolower('О Нас')){
        $about=$content->title;
    }

both of them didn`t work. if I make both strings in the same 'case' it returns true, otherwise false. $content->title returning 'О Нас' from Mysql databse and second string is also 'О Нас' but if I make second str 'О нас' and use one of the above comparison it returns false, why? any suggestions?

Upvotes: 2

Views: 1464

Answers (1)

Ruslan Stelmachenko
Ruslan Stelmachenko

Reputation: 5429

I don't know why, but found one working solution using mb_strtolower function:

mb_strtolower($content->title) == mb_strtolower('О Нас')

My guess about why the first option does not work:

From strtolower docs:

Note that 'alphabetic' is determined by the current locale. This means that e.g. in the default "C" locale, characters such as umlaut-A (Ä) will not be converted.

From mb_strtolower docs:

By contrast to strtolower(), 'alphabetic' is determined by the Unicode character properties. Thus the behaviour of this function is not affected by locale settings and it can convert any characters that have 'alphabetic' property, such as A-umlaut (Ä).

Also make sure that both comparable strings use the same encoding (string literal uses source file encoding) or you can use second parameter of mb_strtolower function to set encoding of each string.

Upvotes: 2

Related Questions