2024 年 PHP Conference Japan

mb_ucfirst

(PHP 8 >= 8.4.0)

mb_ucfirst將字串的第一個字元轉換為大寫

說明

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

執行多位元組安全的 ucfirst() 操作,並返回一個字串,其中 string 的第一個字元被轉換為大寫。

參數

string
輸入字串。
encoding
字串編碼。

返回值

返回結果字串。

注意事項

注意:

與標準的大小寫轉換函式(例如 strtolower()strtoupper())相比,大小寫轉換是基於 Unicode 字元屬性執行的。因此,此函式的行為不受地區設定的影響,它可以轉換任何具有「字母」屬性的字元,例如 a-umlaut (ä)。

有關 Unicode 屬性的更多資訊,請參閱 » http://www.unicode.org/reports/tr21/

參見

新增註解

使用者貢獻的註解 1 則註解

hans at loltek dot net
13 天前
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);
}

}
?>

如果您想知道為什麼我要使用 mb_internal_encoding:在 php7 之前,$encoding 不可為 null。如果您的 polyfill 不需要支援 php5.6,您可以將其刪除。
To Top