Hello! I often have problems with encodings, in particular with the display of Russian characters in all more or less modern browsers. Here is another question: Why doesn't the PHP function "ucfirst ()" work for Russian characters? How can you make it work? Or is there at least an alternative?

PS Only banalism with the replacement of letters on the array should not be offered.

Thanks to everyone who responded!

    4 answers 4

    UTF-8, of course? PHP cannot work normally with multibyte encodings. The closest solution:

    mb_internal_encoding("UTF-8"); function mb_ucfirst($text) { return mb_strtoupper(mb_substr($text, 0, 1)) . mb_substr($text, 1); } echo mb_ucfirst("тест\n"); 

    Just in case, I will add: if you are still boiling ^ W using CP1251 or, forsake His Pasta, KOI8-R, then the call to setlocale can help, with single-byte encodings ucfirst somehow works:

     setlocale(LC_CTYPE, "ru_RU.CP1251"); echo ucfirst("\xf2\xe5\xf1\xf2\n"); 

    But it will work only if the system has such a locale. Those. if we get lucky.

    • Thank. Everything works fine. I also tried in a similar way: translate the first letter in upper case, but did not work. Thanks again. - AseN 5:58 pm
    • Yes, not at all. You probably forgot when you tried mb_internal_encoding when you tried it - without it, the value from the config is used, and if it is not specified there (which, as a rule, is), then what the hell is going on that nothing works. - drdaeman
    • one
      No, of course I use ONLY "UTF-8". This is the standard. - AseN
    • Yeah, I already understood, as soon as I made it and saw it “works fine”, but I didn’t wash it. Let it be - someone else will come across a question then for sure. And CP1251 sometimes has to be used. There is such an unpleasant phenomenon "support for legacy-systems", yes. Chosen deaf places still KOI8-R standard. True, it's all not in the web, usually. - drdaeman

    If you use utf-8 encoding, then you need the function mb_convert_case .


     $name = 'ярослав'; echo mb_convert_case($name, MB_CASE_TITLE, "UTF-8"); // Ярослав 
    • One minus - puts in the upper case the first letter not only of the first word, but in general all the words in the line. - Cheslab
     $title = 'ASDASDASDASDASD'; $title = mb_strtolower($title, 'UTF-8'); $title = mb_strtoupper(mb_substr($title, 0, 1, 'UTF-8'), 'UTF-8') . mb_substr($title, 1, null,'UTF-8'); echo $title; // 'Asdasdasdasdasd' 
    • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky

    Faced a similar problem - the task to display user names in any encoding (Cyrillic, Latin, etc.) from an array with a capital letter. Invented this solution:

     $names = array("катя", "МАША", "verONIka"); function fix_names($names) { for ($n = 0; $n < 3; $n++) { $names [$n] = mb_strtolower($names[$n]); //все к нижнему регистру echo mb_convert_case("$names[$n]", MB_CASE_TITLE, "UTF-8")."\n"; //выводим с первым символом в верхнем регистре } } fix_names($names); //Катя Маша Veronika 

    But I don’t know how correctly I did everything, since the newbie and I have been learning PHP for a week, but nevertheless, this code works as expected.

    • you just don’t have to do this with PHP: "$names[$n]" , just $names[$n] ideally do not use syntax with double quotes at all (especially where string formatting is not needed), and use sprintf for formatting , works better and saves from neocene bugs. - El '