ucfirst() function for multibyte character encodin

2020-01-26 07:07发布

I've asked about strtolower function. But when using foreign characters it doesn't convert them into uppercase, so I must use:

 mb_strtolower($a,"utf8");

But what can I do, if I want to use ucfirst() function? I haven't found any similar function, where I can set encoding type.

8条回答
仙女界的扛把子
2楼-- · 2020-01-26 08:12

This is more concise solution, although it is rather similar to ucwords function:

$final_string = mb_convert_case($your_string, MB_CASE_TITLE, 'UTF-8');

If you need to capitalize string consist of one word, it is the best solution.

查看更多
够拽才男人
3楼-- · 2020-01-26 08:13

as of 2019-11-18, it seems nobody on stackoverflow got this right, here's how mb_ucfirst() should be implemented in userland:

function mb_ucfirst(string $str, string $encoding = null): string
{
    if ($encoding === null) {
        $encoding = mb_internal_encoding();
    }
    return mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding) . mb_substr($str, 1, null, $encoding);
}
查看更多
登录 后发表回答