php case-insensitive comparison of russian characters

梦想的初衷 提交于 2019-12-11 10:28:19

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/11914158/php-case-insensitive-comparison-of-russian-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!