Laravel Live Japan

mb_ucfirst

(PHP 8 >= 8.4.0)

mb_ucfirst文字列の最初の文字を大文字にする

説明

mb_ucfirst(string $string, ?string $encoding = null): string

マルチバイト対応の ucfirst() 処理を行い、 string の最初の文字をタイトルケースに変換して返します。

パラメータ

string
入力文字列。
encoding
文字エンコーディングを指定します。省略した場合、もしくはnullの場合は、内部文字エンコーディングを使用します。

戻り値

変換後の文字列を返します。

注意

注意:

strtolower()strtoupper()のような関数とは対照的に、 ケースフォールディングはUnicode 文字属性を基準に行われます。 したがって、この挙動はロケールの設定に影響されず、また、すべてのアルファベット、 例えば a ウムラウト (ä)などを変換することができます。

Unicodeプロパティについての詳細はこちらを参照してください» http://www.unicode.org/reports/tr21/

参考

  • mb_lcfirst() - 文字列の最初の文字を小文字にする
  • mb_convert_case() - 文字列に対してケースフォールディングを行う
  • ucfirst() - 文字列の最初の文字を大文字にする
add a note

User Contributed Notes 2 notes

up
0
empiredesrtroyer12 at gmail dot com
14 days ago
For non-english words enconded in utf-8 works mb_convert_case  with MB_CASE_TITLE option

mb_convert_case(mb_substr($str, 0, 1), MB_CASE_TITLE) . mb_substr($str, 1);

For a single word 

echo mb_convert_case('çağla', MB_CASE_TITLE);

https://stackoverflow.com/questions/25729900/ucfirst-doesnt-work-on-non-english-characters
up
-1
hans at loltek dot net
1 year ago
polyfill:

<?php
if(PHP_VERSION_ID < 80400) {
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);
}

}
?>

if you wonder why i bother with mb_internal_encoding: prior to php7, $encoding was not nullable. if your polyfill don't need php5.6 support, you can drop it.
To Top