虽然这是一个相当老的问题,但是:
function titleCase($string, $delimiters = array(" ", "-", ".", "'", "O'", "Mc", "Mac"), $exceptions = array("and", "to", "of", "das", "dos", "de", "do", "da", "los", "von", "van", "I", "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
*/
$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");
} else if (in_array(mb_strtolower($word, "UTF-8"), $exceptions)) {
// check exceptions list for any words that should be in lower case
$word = mb_strtolower($word, "UTF-8");
} else if (!in_array($word, $exceptions)) {
// convert to uppercase (non-utf8 only)
$word = ucfirst($word);
}
array_push($newwords, $word);
}
$string = join($delimiter, $newwords);
} //foreach
return $string;
}
它不适用于永成,但它几乎适用于其他任何东西。唯一的问题是,如果$string
只有像“do Carmo”这样的姓氏,那么它将返回“Do Carmo”。它真的是为全名而构建的,所以如果你$string = "frederick do carmo";
它会返回“Frederick do Carmo”。希望这会有所帮助。