phpstringspecial-charactersucfirst

ucfirst() not working properly with scandinavic characters


How to get ucfirst() working with scandinavic characters?

$str = "SÄKYLÄ";
echo ucfirst(strtolower($str)); //prints SÄkylÄ

One possibility is use mb_convert_case() but I'd like to know if this is possible using ucfirst()

$str = "SÄKYLÄ";
echo mb_convert_case($str, MB_CASE_TITLE, "UTF-8"); //prints Säkylä

Which function is faster for string capitalization?


Solution

  • Your problem here is not ucfirst() it's strtolower(). You have to use mb_strtolower(), to get your string in lower case, e.g.

    echo ucfirst(mb_strtolower($str));
               //^^^^^^^^^^^^^^ See here
    

    Also you can find a multibyte version of ucfirst() in the comments from the manual:

    Simple multi-bytes ucfirst():

    <?php
    
       function my_mb_ucfirst($str) {
           $fc = mb_strtoupper(mb_substr($str, 0, 1));
           return $fc.mb_substr($str, 1);
       }
    

    Code from plemieux from the manual comment