PHP Conference Japan 2024

mb_convert_case

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

mb_convert_case對字串執行大小寫轉換

描述

mb_convert_case(字串 $string, 整數 $mode, ?字串 $encoding = null): 字串

根據 mode 指定的方式,對 字串 執行大小寫轉換。

參數

字串

正在轉換的 字串

模式

轉換模式。可以是 MB_CASE_UPPERMB_CASE_LOWERMB_CASE_TITLEMB_CASE_FOLDMB_CASE_UPPER_SIMPLEMB_CASE_LOWER_SIMPLEMB_CASE_TITLE_SIMPLEMB_CASE_FOLD_SIMPLE 的其中之一。

編碼

encoding 參數是字元編碼。如果省略或為 null,則會使用內部字元編碼值。

回傳值

mode 指定的方式轉換的 string 的大小寫轉換版本。

變更日誌

版本 描述
8.3.0 實作希臘字母 sigma 的條件式大小寫規則,這些規則僅適用於 MB_CASE_LOWERMB_CASE_TITLE 模式,不適用於 MB_CASE_LOWER_SIMPLEMB_CASE_TITLE_SIMPLE
7.3.0 新增對 MB_CASE_FOLDMB_CASE_UPPER_SIMPLEMB_CASE_LOWER_SIMPLEMB_CASE_TITLE_SIMPLEMB_CASE_FOLD_SIMPLE 作為 mode 的支援。

範例

範例 1 mb_convert_case() 範例

<?php
$str
= "mary had a Little lamb and she loved it so";
$str = mb_convert_case($str, MB_CASE_UPPER, "UTF-8");
echo
$str; // 印出 MARY HAD A LITTLE LAMB AND SHE LOVED IT SO
$str = mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
echo
$str; // 印出 Mary Had A Little Lamb And She Loved It So
?>

範例 2 mb_convert_case() 使用非拉丁 UTF-8 文字的範例

<?php
$str
= "Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός";
$str = mb_convert_case($str, MB_CASE_UPPER, "UTF-8");
echo
$str; // 印出 ΤΆΧΙΣΤΗ ΑΛΏΠΗΞ ΒΑΦΉΣ ΨΗΜΈΝΗ ΓΗ, ΔΡΑΣΚΕΛΊΖΕΙ ΥΠΈΡ ΝΩΘΡΟΎ ΚΥΝΌΣ
$str = mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
echo
$str; // 印出 Τάχιστη Αλώπηξ Βαφήσ Ψημένη Γη, Δρασκελίζει Υπέρ Νωθρού Κυνόσ
?>

注意事項

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

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

參見

新增註解

使用者提供的註解 10 則註解

alNzy
4 年前
您可以使用此函式來修正與土耳其文的 "ı"、"I"、"i"、"İ" 字元相關的問題。此函式也會將奇怪的 "i̇" 字元取代為一般的 "i" 字元 ("i̇ => i")。

function mb_convert_case_tr($str, $type, $encoding = "UTF-8")
{

switch ($type) {
case "u"
case "upper"
case MB_CASE_UPPER
$type = MB_CASE_UPPER;
break;
case "l"
case "lower"
case MB_CASE_LOWER
$type = MB_CASE_LOWER;
break;
case "t"
case "title"
case MB_CASE_TITLE
$type = MB_CASE_TITLE;
break;
}

$str = str_replace("i", "İ", $str);
$str = str_replace("I", "ı", $str);

$str = mb_convert_case($str, $type, $encoding);
$str = str_replace("i̇", "i", $str);

return $str;
}
agash at freemail dot hu
15 年前
由於先前發布的此函式版本無法處理 UTF-8 字元,我只是嘗試將 ucfirst 取代為 mb_convert_case,但是隨後在迴圈處理分隔符號時,任何先前的大小寫轉換都會遺失。
因此,我決定對輸入字串執行 mb_convert_case(它還處理大寫的單字,這些單字在執行區分大小寫的搜尋時也可能會有問題),然後在之後執行其餘的檢查。

與 mb_convert_case 一樣,單字會大寫,我也為例外新增了小寫轉換,但是,基於上述原因,我保留了 ucfirst 不變。

現在它也適用於 utf-8 字串,但以 UTF-8 字元結尾的字串分隔符號除外("Mcádám" 不變,而 "mcdunno's" 會轉換為 "McDunno's",而 "ökrös-TÓTH éDUa" 也會放入正確的格式中)

我將它用於檢查使用者在姓名和地址上的輸入,因此例外清單也包含一些匈牙利文單字。

<?php

function titleCase($string, $delimiters = array(" ", "-", ".", "'", "O'", "Mc"), $exceptions = array("út", "u", "s", "és", "utca", "tér", "krt", "körút", "sétány", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX", "XXI", "XXII", "XXIII", "XXIV", "XXV", "XXVI", "XXVII", "XXVIII", "XXIX", "XXX" )) {
/*
* Exceptions in lower case are words you don't want converted
* Exceptions all in upper case are any words you don't want converted to title case
* but should be converted to upper case, e.g.:
* king henry viii or king henry Viii should be King Henry VIII
*/
$string = mb_convert_case($string, MB_CASE_TITLE, "UTF-8");

foreach (
$delimiters as $dlnr => $delimiter){
$words = explode($delimiter, $string);
$newwords = array();
foreach (
$words as $wordnr => $word){

if (
in_array(mb_strtoupper($word, "UTF-8"), $exceptions)){
// check exceptions list for any words that should be in upper case
$word = mb_strtoupper($word, "UTF-8");
}
elseif (
in_array(mb_strtolower($word, "UTF-8"), $exceptions)){
// check exceptions list for any words that should be in upper case
$word = mb_strtolower($word, "UTF-8");
}

elseif (!
in_array($word, $exceptions) ){
// convert to uppercase (non-utf8 only)

$word = ucfirst($word);

}
array_push($newwords, $word);
}
$string = join($delimiter, $newwords);
}
//foreach
return $string;
}

?>
Rasa Ravi at tantrajoga dot cz
19 年前
針對捷克字元
<?php
$text
= mb_convert_case($text, MB_CASE_LOWER, "Windows-1251");
?>
正確的編碼 Windows-1250 無效(請參閱 mb_list_encodings 列表),但 Windows-1251 可以 100% 達到相同的效果。函式 strtolower() 會忽略帶有變音符號的捷克字元。
info at yasarnet dot com
16 年前
在我的情況下,以下程式碼可以將 UTF-8 編碼的字串轉換為首字大寫。

function capitalize($str, $encoding = 'UTF-8') {
return mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding) . mb_strtolower(mb_substr($str, 1, mb_strlen($str), $encoding), $encoding);
}
dave at wp dot pl
8 年前
MB_CASE_TITLE 不會變更引號內的字母。

範例
mb_convert_case('AAA "aaa"', MB_CASE_TITLE);
// 結果:Aaa "aaa"
the at psychoticneurotic dot com
15 年前
在 Justin 和 Alex 的工作基礎上...

此函式允許您指定要以哪個分隔符號來分割字串(不只是預設的空格)。現在您可以正確地將愛爾蘭姓名和連字號單字轉換為首字大寫(如果您想要的話)!

<?php
function titleCase($string, $delimiters = array(" ", "-", "O'"), $exceptions = array("to", "a", "the", "of", "by", "and", "with", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X")) {
/*
* Exceptions in lower case are words you don't want converted
* Exceptions all in upper case are any words you don't want converted to title case
* but should be converted to upper case, e.g.:
* king henry viii or king henry Viii should be King Henry VIII
*/
foreach ($delimiters as $delimiter){
$words = explode($delimiter, $string);
$newwords = array();
foreach (
$words as $word){
if (
in_array(strtoupper($word), $exceptions)){
// check exceptions list for any words that should be in upper case
$word = strtoupper($word);
} elseif (!
in_array($word, $exceptions)){
// convert to uppercase
$word = ucfirst($word);
}
array_push($newwords, $word);
}
$string = join($delimiter, $newwords);
}
return
$string;
}
?>
turabgarip at gmail dot com
6 個月前
與其他字串函式一樣,此函式在處理土耳其文的「i」時會出現問題。關於這個問題,2015 年就有錯誤回報,但 PHP 團隊表示「尚未實作特定於語言的條件特殊案例對應」,雖然這實際上破壞了函式的邏輯,並使其無法用於此目的。

https://bugs.php.net/bug.php?id=70072

問題源於拉丁文中的字母「i」與土耳其文中的「i」是完全不同的字母。土耳其文的「ı」大寫會變成「I」;而拉丁文的「I」大寫實際上是「i」的大寫,而不是「ı」。

PHP 在某些情況下會考慮到這一點,但在其他情況下會忽略它;這會導致不可預測的行為。當有問題的字母位於單字的中間或開頭時,當某些多位元組字元與標準拉丁字元或另一個多位元組字元相鄰時,等等。這些行為都不同,這是不對的。

有一些使用者註解試圖涵蓋這一點,但效率不高。因為它們中的一些沒有涵蓋單字邊界,而另一些則會產生非標準字元。以下是我測試並使用了一段時間的方法:

<?php

function mb_convert_case_i(string $string, int $mode = MB_CASE_TITLE, string $encoding = 'UTF-8'): string {
// 土耳其文的 "i" 是一個特例
$string = match($mode) {
MB_CASE_UPPER, MB_CASE_UPPER_SIMPLE => str_replace(['i', 'ı'], ['İ', 'I'], $string),
MB_CASE_LOWER, MB_CASE_LOWER_SIMPLE => str_replace(['İ', 'I'], ['i', 'ı'], $string),
// 當 i 和 ı 出現在單字開頭時,PHP 的行為會有所不同
MB_CASE_TITLE, MB_CASE_TITLE_SIMPLE => preg_replace(['/İ/u', '/I/u', '/\b(i)/u'], ['i', 'ı', 'İ'], $string),
default =>
$string,
};
return
mb_convert_case($string, $mode, $encoding);
}

?>

如您所見,它使用了 match 語法,這需要 PHP 8。對於較低版本,您可以使用 switch 語法來適當替換。我沒有測試它的大小寫摺疊 (case folding) 功能。如果您需要它,只需在 match 中添加另一個條件即可。
tavhane at gmail dot com
6 年前
對於土耳其語的簡化版本

$str = mb_convert_case(str_replace(['i','I'], ['İ','ı'], $str), MB_CASE_TITLE,"UTF-8");
Anonymous
3 年前
$str = "Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός";
$str = mb_convert_case($str, MB_CASE_UPPER, "UTF-8");
這個轉換沒有產生你已經發佈的範例
而是產生這個

$str = mb_convert_case($str, MB_CASE_UPPER, "UTF-8");
"ΤΆΧΙΣΤΗ ΑΛΏΠΗΞ ΒΑΦΉΣ ΨΗΜΈΝΗ ΓΗ, ΔΡΑΣΚΕΛΊΖΕΙ ΥΠΈΡ ΝΩΘΡΟΎ ΚΥΝΌΣ"
webenformasyon at gmail dot com
6 年前
對於土耳其語,I => i 和 i => I 的轉換是個問題。它應該是 I => ı 和 i => İ,所以我的簡單解決方案是

public function title_case_turkish($str){


$str = str_replace("i", "İ", $str);
$str = str_replace("I", "ı", $str);

$str = mb_convert_case($str, MB_CASE_TITLE,"UTF-8");

return $str;

}
To Top