PHP function: mb_ucfirst


Currently on PHP does not have a multibyte (UTF-8) version of ucfirst function. I wrote some multibyte mb_ucfirst function:

You must call these functions before:

mb_internal_encoding("UTF-8");  // before calling the function
mb_regex_encoding("UTF-8");

Method 1:

if (!function_exists('mb_ucfirst') && function_exists('mb_substr') && function_exists('mb_strtoupper')) {
    function mb_ucfirst($string) {
        $string = mb_strtoupper(mb_substr($string, 0, 1)) . mb_substr($string, 1);
        return $string;
    }
}

Method 2:

if (!function_exists('mb_ucfirst') && function_exists('mb_substr') && function_exists('mb_strtoupper') && function_exists('mb_strtolower') && function_exists('mb_strlen')) {
    function mb_ucfirst($string, $e ='UTF-8') {         
        $string = mb_strtolower($string, $e);
        $upper = mb_strtoupper($string, $e);
        preg_match('#(.)#us', $upper, $matches);
        $string = $matches[1] . mb_substr($string, 1, mb_strlen($string, $e), $e);         
        return $string;
    } 
}

Method 3:

if (!function_exists('mb_ucfirst') && function_exists('mb_strtoupper')) {
    function mb_ucfirst($str){
        preg_match_all("~^(.)(.*)$~u", $str, $arr);
        return mb_strtoupper($arr[1][0]).$arr[2][0];
    } 
}

Method 4:

if (!function_exists('mb_ucfirst') && function_exists('mb_strtolower') && function_exists('mb_strtoupper') && function_exists('mb_substr') && function_exists('mb_strlen'))
{
    function mb_ucfirst($str, $encoding = "UTF-8", $lower_str_end = false)
    {
        $first_letter = mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding);
        $str_end = "";
        if ($lower_str_end)
        {
            $str_end = mb_strtolower(mb_substr($str, 1, mb_strlen($str, $encoding), $encoding), $encoding);
        } else
        {
            $str_end = mb_substr($str, 1, mb_strlen($str, $encoding), $encoding);
        }
        $str = $first_letter . $str_end;
        return $str;
    }
}

Method 5: New update 12/02/2015

if (!function_exists('mb_ucfirst') && function_exists('mb_ereg_replace') && function_exists('mb_strtoupper'))
{
    function mb_ucfirst($str)
    {
        return mb_ereg_replace("^(\w)","mb_strtoupper(\"\\1\")",$str,'e');
    }
}

Example:

echo mb_ucfirst("картина");

Result:

Картина

How to make a string’s first character Uppercase-Multibyte?

Recent search terms:

  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1841 MtXbJgkHqYy4kG4WFM1bCPHupwpYIp0Ott2FXSrhty1I1m976jQCZJiAQparms0q 713363c0ec4af0dc84739c2d2b7092d5948417ca&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1832 _6CTlFIu7yjyDMhqCv42jmQM75iZ985qBnj3KxatiRPWxhvFTKF_M8fbFgGFbjKX f682590645029fed55311ee26e27f0cf8dbe0549&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1831 rmbiJfpRdoZAvLOPdPNXH-3S07cGXf-5d3VeRbvzKYsQ8Us4gQemY3OK5EyiSuSG 52c0fdf6d75ff9f3d24b4b1dd749076328914f29&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1830 PtO8kYFbuHAfU91hOTGLj3zyIGkxwaCH6RQE7YECQYyxIyQ6qTAdpiMdeswl-kP1 98e5c065d08bb91fe81246d2280d89201b1f798c&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1830 mO7pA_0HNCCTBgiI0rY7K0yhBO-9LF71UTvdZiSROquwji4QrVO5joyU9Mv7Wc1k 7f7504516b84c9f97e7357d06d5931f308caaa5b&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1830 FTvevmMNcyeJ7G-6iO3S7_IeLbXZQ-r3ap-w1ggYTv8-vsMjznCS9uTtRng0jLkI 28f1bba88fc2c5157891615f241053a6a7c1883a&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1829 kOzBjiXYpNALbhNtVamLYttCF_eoAu1amlGarjx81rh6EemoxOVvn-GheLfM42MQ 4ba5faf7080a7fe8810af55eba157b4da5dd9066&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1827 -d3tKPJNU8RmbvFK-W0nZUigs6S6S8DLW2HJh19OQEkN4rqB3iMxB5L1YCV9xT2B 2bc4027efd7b319646cf12cc8210c5c7d1709f8a&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1823 SU8p7AvyOJwDE7Jc8u3lEiiEzC6XuuH9K9NHdHZYobWYd2-RgkDDiCNWLmD7cqpT d5f93fbde8b35e8a9f1050c11e8f8d432af4fe57&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1820 MTdfB44gRtqy367fSJjGMAKq2kULQALsoGwOUi9k2lUjpCKDSCSWT8WlAEyFpjaa 4d6f67e44768a887383582d8f6d692653ba7791f&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme

Leave a Reply