PHP Conference Japan 2024

ucfirst

(PHP 4, PHP 5, PHP 7, PHP 8)

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

說明

ucfirst(字串 $string): 字串

如果 string 的第一個字元是 ASCII 字元,範圍從 "a" (0x61) 到 "z" (0x7a),則返回將該字元大寫後的字串。

參數

字串

輸入字串。

返回值

返回結果字串。

更新日誌

版本 說明
8.2.0 大小寫轉換不再依賴於使用 setlocale() 設定的地區設定。只有 ASCII 字元會被轉換。

範例

範例 #1 ucfirst() 範例

<?php
$foo
= 'hello world!';
$foo = ucfirst($foo); // Hello world!

$bar = 'HELLO WORLD!';
$bar = ucfirst($bar); // HELLO WORLD!
$bar = ucfirst(strtolower($bar)); // Hello world!
?>

參見

新增註釋

使用者貢獻的註釋 34 則註釋

plemieux
19 年前
簡單的多位元組 ucfirst()

<?php
function my_mb_ucfirst($str) {
$fc = mb_strtoupper(mb_substr($str, 0, 1));
return
$fc.mb_substr($str, 1);
}
?>
prokur.net - there is my email
16 年前
我相信 mb_ucfirst 很快就會被加入 PHP,但目前這可能很有用
<?php

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

?>

它也檢查是否啟用了多位元組支援。
Ekin
4 年前
由於多位元組字元,這個函式不適用於土耳其語。但你可以使用一些技巧。

<?php
function ucfirst_tr($str) {
$trMap = ['Ğ'=>'ğ','Ü'=>'ü','Ş'=>'ş','İ'=>'i','Ö'=>'ö','Ç'=>'ç','I'=>'ı'];
$str = mb_strtolower(strtr($str, $trMap));
$first = mb_substr($str, 0, 1);
$first = strtr($first, array_flip($trMap));
$first = mb_strtoupper($first);
return
$first . mb_substr($str, 1);
}
?>
qeremy [atta] gmail [dotta] com
12 年前
一個正確的土耳其語解決方案;

<?php
函數 ucfirst_turkish($str) {
$tmp = preg_split("//u", $str, 2, PREG_SPLIT_NO_EMPTY);
返回
mb_convert_case(
str_replace("i", "İ", $tmp[0]), MB_CASE_TITLE, "UTF-8").
$tmp[1];
}

$str = "iyilik güzelLİK";
echo
ucfirst($str) ."\n"; // 輸出 Iyilik güzelLİK
echo ucfirst_turkish($str); // 輸出 İyilik güzelLİK
?>
mattalexxpub at gmail dot com
16 年前
這是我的字串轉換成句子大小寫的用法

<?php
函數 sentence_case($string) {
$sentences = preg_split('/([.?!]+)/', $string, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
$new_string = '';
foreach (
$sentences 作為 $key => $sentence) {
$new_string .= ($key & 1) == 0?
ucfirst(strtolower(trim($sentence))) :
$sentence.' ';
}
返回
trim($new_string);
}

print
sentence_case('HMM. WOW! WHAT?');

// 輸出: "Hmm. Wow! What?"
?>
mingalevme at gmail dot com
11 年前
針對「多詞」字串的多位元組 ucfirst 實作 (需要 mbstring 模組)

<?php

public static function ucfirst($str)
{
$str = mb_strtolower($str);
$words = preg_split('/\b/u', $str, -1, PREG_SPLIT_NO_EMPTY);
foreach (
$words as $word) {
$ucword = mb_strtoupper(mb_substr($word, 0, 1)) . mb_substr($word, 1);
$str = str_replace($word, $ucword, $str);
}
return
$str;
}

?>
charliefortune
16 年前
這個函式可以將姓名的每個部分的首字母大寫,並將其餘部分轉換為小寫。您可以傳入您想要用作分隔符的字元。

例如:<?php echo nameize("john o'grady-smith"); ?>

會回傳 John O'Grady-Smith

<?php

function nameize($str,$a_char = array("'","-"," ")){
// $str 包含完整的原始名稱字串
//$a_char 是一個陣列,包含我們用作大寫分隔符的字元。如果您沒有傳入任何值,則預設包含三個字元。
$string = strtolower($str);
foreach (
$a_char as $temp){
$pos = strpos($string,$temp);
if (
$pos){
// 我們在迴圈中,因為我們在陣列中找到了其中一個特殊字元,所以讓我們將其拆分成多個區塊,並將每個區塊的第一個字母大寫。
$mend = '';
$a_split = explode($temp,$string);
foreach (
$a_split as $temp2){
// 將以特殊字元分隔的字串的每個部分的第一個字母大寫
$mend .= ucfirst($temp2).$temp;
}
$string = substr($mend,0,-1);
}
}
return
ucfirst($string);
}

?>
Bartuc
18 年前
這是針對土耳其語字母表的修正函式。

<?php

function uc_first($str){
$str[0] = strtr($str,
"abcdefgh?ijklmnopqrstuvwxyz".
"\x9C\x9A\xE0\xE1\xE2\xE3".
"\xE4\xE5\xE6\xE7\xE8\xE9".
"\xEA\xEB\xEC\xED\xEE\xEF".
"\xF0\xF1\xF2\xF3\xF4\xF5".
"\xF6\xF8\xF9\xFA\xFB\xFC".
"\xFE\xFF",
"ABCDEFGHI?JKLMNOPQRSTUVWXYZ".
"\x8C\x8A\xC0\xC1\xC2\xC3\xC4".
"\xC5\xC6\xC7\xC8\xC9\xCA\xCB".
"\xCC\xCD\xCE\xCF\xD0\xD1\xD2".
"\xD3\xD4\xD5\xD6\xD8\xD9\xDA".
"\xDB\xDC\xDE\x9F");
return
$str;
}

?>
nospam at nospam dot com
7 年前
改良後的句子首字母大寫方法。
前兩個操作(雙空格和全大寫)是可選的,因此可以移除而不影響功能。

<?php
// 傳回句子首字母大寫的字串
function ucsentence($str) {
if (
$str) { // 輸入字串
$str = preg_replace('/'.chr(32).chr(32).'+/', chr(32), $str); // 遞迴地將所有雙空格替換為單空格
if (($x = substr($str, 0, 10)) && ($x == strtoupper($x))) $str = strtolower($str); // 取前 10 個字元作為樣本,如果全是大寫,則將 $str 轉換為小寫;如果總是執行此操作,則任何正確的大寫字母都會遺失
$na = array('. ', '! ', '? '); // 標點符號 needles
foreach ($na as $n) { // 每個標點符號 needle
if (strpos($str, $n) !== false) { // 找到標點符號 needle
$sa = explode($n, $str); // 分割字串
foreach ($sa as $s) $ca[] = ucfirst($s); // 將每個子字串的首字母大寫
$str = implode($n, $ca); // 使用重建後的版本替換 $str
unset($ca); // 清除陣列以供下次迴圈使用
}
}
return
ucfirst(trim($str)); // 如果找不到標點符號 needles,則將第一個字母大寫
}
}
?>

"heLLo EarthLing!" >> "HeLLo EarthLing!"
"I'M MOSTLY. caps! " >> "I'm mostly. Caps!"
「ALLCAPS」 >> 「Allcaps」
「i haVe neST.ed punct,u.ation! sp A c es. and CAPs.. 」 >> 「I haVe neST.ed punct,u.ation! Sp A c es. And CAPs..」
Carel at divers information with dotcom
17 年前
我做了一個小修改。現在它會處理數字中的小數點。

function ucsentence ($string){
$string = explode ('.', $string);
$count = count ($string);
for ($i = 0; $i < $count; $i++){
$string[$i] = ucfirst (trim ($string[$i]));
if ($i > 0){
if ((ord($string[$i]{0})<48) || (ord($string[$i]{0})>57)) {
$string[$i] = ' ' . $string[$i];
}
}
}
$string = implode ('.', $string);
return $string;
}
kiprasbal at gmail dot com
10 年前
我的版本,將字串中第一個單字的首字母轉換為大寫。

public function mb_ucfirst($str) {
$aParts = explode(" ",$str);
$firstWord = mb_convert_case($aParts[0],MB_CASE_TITLE,"UTF-8");
unset($aParts[0]);

return $firstWord." ".implode(" ",$aParts);
}
Quicker
13 年前
如果您想對 UTF-8 字串使用 ucfirst,請嘗試這個方法。

<?php
function ucfirst_utf8($stri){
if(
$stri{0}>="\xc3")
return ((
$stri{1}>="\xa0")?
(
$stri{0}.chr(ord($stri{1})-32)):
(
$stri{0}.$stri{1})).substr($stri,2);
else return
ucfirst($stri);
}
?>

它很快,不依賴語言(但依賴 UTF-8),並且不使用任何 mb 函數,例如 mb_ucfirst。
pete at namecube dot net
14 年前
對於任何想要將句子中每個單字的首字母大寫的人來說,這個方法對我有效。

<?php
function ucfirst_sentence($str)
{
return
preg_replace('/\b(\w)/e', ‘strtoupper("$1")', $str);
}
?>
octavius
15 年前
對於使用 UTF-8 編碼的立陶宛語文本,我使用兩個函數(感謝 [mattalexxpub at gmail dot com] 和 Svetoslav Marinov)

<?php
function my_ucfirst($string, $e ='utf-8') {
if (
function_exists('mb_strtoupper') && function_exists('mb_substr') && !empty($string)) {
$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);
}
else {
$string = ucfirst($string);
}
return
$string;
}

function
sentence_case($string) {
$sentences = preg_split('/([.?!]+)/', $string, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
$new_string = '';
foreach (
$sentences as $key => $sentence) {
$new_string .= ($key & 1) == 0?
my_ucfirst(strtolower(trim($sentence))) :
$sentence.' ';
}
return
trim($new_string);
}
?>
匿名
7 年前
格式化輸入字串

<?php

function ucsentences($string){
$parts = preg_split('/([^\.\!\?;]+[\.\!\?;"]+)/', strtolower($string), (-1), PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
$r = '';
foreach(
$parts as $key=>$sentence){
$r .= ucfirst(trim($sentence)) . ' ';
}
$r = preg_replace('/\bi\b/', 'I', $r);
$r = preg_replace_callback('/("[a-z])/', function($m){ return strtoupper($m[0]);}, $r);
return
rtrim($r);
}

$str = 'i\'m not sure. if this is good enough, but i thought: "hey, who know\'s. maybe i am right."';

?>
輸出
I'm not sure. If this is good enough, but I thought: "Hey, who know's. Maybe I am right."
vlknmtn at gmail dot com
13 年前
土耳其文解決方案

<?php
mb_internal_encoding
("UTF-8");
mb_regex_encoding("UTF-8");

function
tr_ilkbuyuk($text)
{
$text = str_replace("I","ı",$text);
$text = mb_strtolower($text, 'UTF-8');

if(
$text[0] == "i")
$tr_text = "İ".substr($text, 1);
else
$tr_text = mb_convert_case($text, MB_CASE_TITLE, "UTF-8");

return
trim($tr_text);
}

function
tr_ucwords($text)
{
$p = explode(" ",$text);
if(
is_array($p))
{
$tr_text = "";
foreach(
$p AS $item)
$tr_text .= " ".tr_ilkbuyuk($item);

return
trim($tr_text);
}
else
return
tr_ilkbuyuk($text);
}

$deger = "ıişllşlsdg";

echo
tr_ucwords($deger);

?>
Markus Ernst
18 年前
plemieux 的函式在我沒有將編碼傳遞給每個 mb 函式的情況下無法正常運作(即使在腳本頂部設定了 ini_set('default_charset', 'utf-8') 也是如此)。以下是在我的應用程式(PHP 4.3)中可以運作的範例:

<?php
function my_mb_ucfirst($str, $e='utf-8') {
$fc = mb_strtoupper(mb_substr($str, 0, 1, $e), $e);
return
$fc.mb_substr($str, 1, mb_strlen($str, $e), $e);
}
?>
svetoslavm at gmail dot com
16 年前
基於某些原因,這個方法對我來說有效。

Mac OS 10.5.1
PHP 5.2.6

<?php
/**
* 針對 UTF-8 的 ucfirst 函式
*
* @param string $string
* @return string
* @see http://ca.php.net/ucfirst
*/
function my_ucfirst($string, $e ='utf-8') {
if (
function_exists('mb_strtoupper') && function_exists('mb_substr') && !empty($string)) {
$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);
} else {
$string = ucfirst($string);
}
return
$string;
}
?>

Svetoslav Marinov
http://slavi.biz
bgschool
15 年前
用於處理 UTF-8 編碼斯拉夫語系文字的簡易 ucfirst 函式

<?php
public function capitalize_first($str) {
$line = iconv("UTF-8", "Windows-1251", $str); // 轉換為 windows-1251
$line = ucfirst($line);
$line = iconv("Windows-1251", "UTF-8", $line); // 轉換回 utf-8

return $line;
}
?>
wilfried dot loche at fr dot adp dot com
14 年前
如果有人在找 Oracle 資料庫中的對應函式,那就是:INITCAP。希望這有幫助!
webmaster at onmyway dot cz
16 年前
受到 lcfirst 函式的啟發,一個簡單的 mb_lcfirst 來處理多位元組字串

<?php
函數 mb_lcfirst($str, $enc = null)
{
如果(
$enc === null) $enc = mb_internal_encoding();
返回
mb_strtolower(mb_substr($str, 0, 1, $enc), $enc).mb_substr($str, 1, mb_strlen($str, $enc), $enc);
}
?>
adefoor at gmail dot com
17 年前
Ken 和 zee

為了讓這個函式更通用,我會在你的 $sentence 周圍加上 strtolower()。這樣可以轉換全大寫和全小寫的文字區塊。

<?php

函數 sentence_cap($impexp, $sentence_split) {
$textbad=explode($impexp, $sentence_split);
$newtext = 陣列();
foreach ($textbad as $sentence) {
$sentencegood=ucfirst(strtolower($sentence));
$newtext[] = $sentencegood;
}
$textgood = implode($impexp, $newtext);
返回
$textgood;
}

$text = "this is a sentence. this is another sentence! this is the fourth sentence? no, this is the fourth sentence.";
$text = sentence_cap(". ",$text);
$text = sentence_cap("! ",$text);
$text = sentence_cap("? ",$text);

echo
$text; // This is a sentence. This is another sentence! This is the fourth sentence? No, this is the fourth sentence.

?>
Markus Ernst
18 年前
以下函數的組合,可在共享主機環境(您無法始終依賴已安裝 mbstring 的情況下)對多位元組字串啟用 ucfirst 功能。

<?php
function my_mb_ucfirst($str, $e='utf-8') {
if (
function_exists('mb_strtoupper')) {
$fc = mb_strtoupper(mb_substr($str, 0, 1, $e), $e);
return
$fc.mb_substr($str, 1, mb_strlen($str, $e), $e);
}
else {
$str = utf8_decode($str);
$str[0] = strtr($str[0],
"abcdefghýijklmnopqrstuvwxyz".
"\x9C\x9A\xE0\xE1\xE2\xE3".
"\xE4\xE5\xE6\xE7\xE8\xE9".
"\xEA\xEB\xEC\xED\xEE\xEF".
"\xF0\xF1\xF2\xF3\xF4\xF5".
"\xF6\xF8\xF9\xFA\xFB\xFC".
"\xFE\xFF",
"ABCDEFGHÝIJKLMNOPQRSTUVWXYZ".
"\x8C\x8A\xC0\xC1\xC2\xC3\xC4".
"\xC5\xC6\xC7\xC8\xC9\xCA\xCB".
"\xCC\xCD\xCE\xCF\xD0\xD1\xD2".
"\xD3\xD4\xD5\xD6\xD8\xD9\xDA".
"\xDB\xDC\xDE\x9F");
return
utf8_encode($str);
}
}
?>
info [at] spwdesign [dot] com
19 年前
這是一段簡單的程式碼,用於從文字中移除所有儲存在資料庫中的「不雅字詞」。您可以使用 str_ireplace,但由於它僅安裝在 PHP5 上,因此這個方法也能運作。它會先將文字轉換為小寫,然後使用 ucfirst() 在它認為應該大寫的地方(在新句子開頭)放置大寫字母。前一個句子以「. 」結尾。

<?php
function filter($text){
$filters=mysql_query("SELECT word,result FROM filter");
while(
$filter=mysql_fetch_array($filters)){
$text=str_replace($filter[word],$filter[result],strtolower($text));
$parts=explode(". ",$text);
for(
$i=0;$i<count($parts);$i++){
$parts[$i]=ucfirst($parts[$i]);
}
$text=implode(". ",$parts);
}
return
$text;
}
?>
匿名
19 年前
啊,上一段程式碼有問題,以下是修正後的版本

<?php

function uc_first($str){
$str[0] = strtr($str,
"abcdefghijklmnopqrstuvwxyz".
"\x9C\x9A\xE0\xE1\xE2\xE3".
"\xE4\xE5\xE6\xE7\xE8\xE9".
"\xEA\xEB\xEC\xED\xEE\xEF".
"\xF0\xF1\xF2\xF3\xF4\xF5".
"\xF6\xF8\xF9\xFA\xFB\xFC".
"\xFD\xFE\xFF",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
"\x8C\x8A\xC0\xC1\xC2\xC3\xC4".
"\xC5\xC6\xC7\xC8\xC9\xCA\xCB".
"\xCC\xCD\xCE\xCF\xD0\xD1\xD2".
"\xD3\xD4\xD5\xD6\xD8\xD9\xDA".
"\xDB\xDC\xDD\xDE\x9F");
return
$str;
}

?>

所以,這個函式也會將其他字母轉換為大寫,`ucfirst()` 只會將 a-z 轉換為 A-Z。
steven at tux dot appstate dot edu
20 年前
注意:當傳入長度為 0 的字串時,此函式的返回值在 4.3 版之後有所變更。在 4.2 之前的版本會返回 false,而在 4.3 之後的版本會返回長度為 0 的字串。

範例

`$name = ucfirst("");`
`var_dump($name);`

`$name = ucfirst("owen");`
`var_dump($name);`

4.2 之前的版本結果
`bool(false) string(4) "Owen"`

4.3 之後的版本結果
`string(0) "" string(4) "Owen"`
Ami Hughes (ami at mistress dot name)
20 年前
如果您需要對多個分隔符號套用相同的動作,您可以使用 `preg_replace` 並將「第二個分隔符號」用實際的分隔符號包起來。

舉例來說,如果您想在設計用於輸入全名的輸入框中使用類似 Lee 的 FormatName 函式,就會遇到這個問題,因為該腳本只設計用於檢查姓氏,就好像它是整個字串一樣。問題是您仍然希望支援雙姓氏,並且您仍然希望能夠支援雙姓氏的第二個部分以「mc」開頭時仍能正確格式化的可能性。

這個範例使用 preg_replace 將分隔符號用您的實際分隔符號括起來。這只是一個非常快速的替代方案,可以取代編寫一些更大更花俏的函式。如果有更簡短、更簡單的方法,歡迎告知我。(強調更簡短和更簡單,因為這就是重點所在。) :D

以下是範例。我刪除了 Lee 的註釋,以免與我的註釋混淆。

<?php

函數 格式化名稱($name=NULL)
{
如果 (empty(
$name))
返回
false;

$name = strtolower($name);
$name = preg_replace("[\-]", " - ",$name); // 將連字號前後加上分隔符號,以便 strncmp 能夠準確判斷
如果 (preg_match("/^[a-z]{2,}$/i",$name)) // 簡單的 preg_match 條件式
{

$names_array = explode(' ',$name); // 設定分隔符號為空格

對於 ($i = 0; $i < count($names_array); $i++)
{
如果 (
strncmp($names_array[$i],'mc',2) == 0 || ereg('^[oO]\'[a-zA-Z]',$names_array[$i]))
{
$names_array[$i][2] = strtoupper($names_array[$i][2]);
}
$names_array[$i] = ucfirst($names_array[$i]);

}

$name = implode(' ',$names_array);
$name = preg_replace("[ \- ]", "-",$name); // 移除多餘的分隔符號
返回 ucwords($name);

}
}

?>
bkimble at ebaseweb dot com
21 年前
這是一個方便的函數,可以將句子中每個單字的首字母大寫。我用它來處理發佈在我網站上的活動標題...我添加了例外情況,用於大寫單字和小寫單字,這樣羅馬數字「IV」就不會被印成「iv」,而像「a」、「the」和「of」這樣的單字仍然是小寫。

函數 移除大寫 ($string)
{
$lower_exceptions = 陣列(
"to" => "1", "a" => "1", "the" => "1", "of" => "1"
);

$higher_exceptions = 陣列(
"I" => "1", "II" => "1", "III" => "1", "IV" => "1",
"V" => "1", "VI" => "1", "VII" => "1", "VIII" => "1",
"XI" => "1", "X" => "1"
);

$words = split(" ", $string); // 將字串 $string 以空格分割成陣列 $words
$newwords = array(); // 建立一個新的空陣列 $newwords

foreach ($words as $word) // 迴圈處理 $words 陣列中的每個元素
{
if (!$higher_exceptions[$word]) // 如果 $word 不在 $higher_exceptions 例外陣列中
$word = strtolower($word); // 將 $word 轉換為小寫
if (!$lower_exceptions[$word]) // 如果 $word 不在 $lower_exceptions 例外陣列中
$word = ucfirst($word); // 將 $word 的首字母轉換為大寫
array_push($newwords, $word); // 將 $word 加入到 $newwords 陣列

}

return join(" ", $newwords); // 將 $newwords 陣列以空格連接成字串並回傳
}
Nethor
12 年前
簡單但可行的解決方案

<?php
mb_internal_encoding
("UTF-8"); // 呼叫函式前設定內部編碼為 UTF-8

function utf8_ucfirst($str){
preg_match_all("~^(.)(.*)$~u", $str, $arr);
return
mb_strtoupper($arr[1][0]).$arr[2][0];
}
?> // UTF-8 環境下的首字母大寫函式
Ken Kehler
17 年前
@ zee:這應該可以解決你的 !、? 和任何你想添加的標點符號問題。 它或許還可以再簡化一些。

<?php

function sentence_cap($impexp, $sentence_split) {
$textbad=explode($impexp, $sentence_split);
$newtext = array();
foreach (
$textbad as $sentence) {
$sentencegood=ucfirst($sentence);
$newtext[] = $sentencegood;
}
$textgood = implode($impexp, $newtext);
return
$textgood;
}

$text = "this is a sentence. this is another sentence! this is the fourth sentence? no, this is the fourth sentence."; // 範例文字
$text = sentence_cap(". ",$text); // 以句點和空格分割並將每個句子首字母大寫
$text = sentence_cap("! ",$text); // 以驚嘆號和空格分割並將每個句子首字母大寫
$text = sentence_cap("? ",$text); // 以問號和空格分割並將每個句子首字母大寫

echo
$text; // 輸出:This is a sentence. This is another sentence! This is the fourth sentence? No, this is the fourth sentence.

?> // 將句子依據指定的標點符號斷句,並將每個句子的首字母大寫
Uwe
17 年前
@adefoor, Ken 和 Zee

大小寫轉換必須理解文字的含義。例如「USA」、「Sunday」、「March」、「I am ...」以及縮寫,像是「prob.」等等。
匿名
18 年前
適用於斯拉夫字母和拉丁字母的簡單函式:

function rucfirst($str) {
if(ord(substr($str,0,1))<192) return ucfirst($str);
else
return chr(ord(substr($str,0,1))-32).substr($str,1);
}
Michael
18 年前
如果 PHP 內建名為 ucsentence 的函式,這就是你預期的效果。

function ucsentence ($string){
$string = explode ('.', $string);
$count = count ($string);
for ($i = 0; $i < $count; $i++){
$string[$i] = ucfirst (trim ($string[$i]));
if ($i > 0){
$string[$i] = '&nbsp;&nbsp;' . $string[$i];
}
}
$string = implode ('.', $string);
return $string;
}
divinity76 at gmail dot com
5 年前
以下是如何在使用者端實作 mb_ucfirst 的方法

<?php

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);
}
?>

(在我撰寫這則評論時,其他所有人的嘗試都因為各種原因而失敗,例如:有些人不允許指定編碼,有些則預設為 utf-8 而不是預設為 mb_internal_encoding() )
To Top