我正在寻找一个可以处理名称正确大写的 php 脚本。虽然我意识到很难处理 100% 的案件
https://en.wikipedia.org/wiki/List_of_family_name_affixes
我认为这个脚本可以很好地处理 95% 的用例,至少对我们来说是这样。这当然是一个很好的起点。
http://www.media-division.com/correct-name-capitalization-in-php/
function titleCase($string)
{
$word_splitters = array(' ', '-', "O'", "L'", "D'", 'St.', 'Mc');
$lowercase_exceptions = array('the', 'van', 'den', 'von', 'und', 'der', 'de', 'da', 'of', 'and', "l'", "d'");
$uppercase_exceptions = array('III', 'IV', 'VI', 'VII', 'VIII', 'IX');
$string = strtolower($string);
foreach ($word_splitters as $delimiter)
{
$words = explode($delimiter, $string);
$newwords = array();
foreach ($words as $word)
{
if (in_array(strtoupper($word), $uppercase_exceptions))
$word = strtoupper($word);
else
if (!in_array($word, $lowercase_exceptions))
$word = ucfirst($word);
$newwords[] = $word;
}
if (in_array(strtolower($delimiter), $lowercase_exceptions))
$delimiter = strtolower($delimiter);
$string = join($delimiter, $newwords);
}
return $string;
}