有没有办法格式化人名?例如,“joHn doe”应该是“John Doe”。或者“angus macgyver”应该是“Angus MacGyver”。等等
我知道任何解决方案都可能不完整(名称规则太多),但有总比没有好。有什么建议么?
有没有办法格式化人名?例如,“joHn doe”应该是“John Doe”。或者“angus macgyver”应该是“Angus MacGyver”。等等
我知道任何解决方案都可能不完整(名称规则太多),但有总比没有好。有什么建议么?
I was looking for a php script that could handle proper capitalization of names. While I realize that it would be difficult to handle 100% of cases
https://en.wikipedia.org/wiki/List_of_family_name_affixes
I think this script does a pretty good job handling 95% of use cases, at least for us. It's certainly a great starting point.
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;
}
正如评论中已经建议的那样,在 PHP 中,您可以执行以下操作:
$name_formatted = ucfirst(strtolower($name_unformatted));
这将处理您 90% 的案件。然后我会将它放入一个函数中并添加规则来处理 MacGuyver、O'Reilly 类型的异常。
更新: 正如所指出的,ucfirst 只处理字符串中的第一个单词。您可以使用正则表达式将每个单词中的所有首字母大写,或者执行如下函数:
<?php
$name_unformatted = "JOHN DOE";
function format_name($name_unformatted)
{
$name_formatted = ucwords(strtolower($name_unformatted)); // this will handle 90% of the names
// ucwords will work for most strings, but if you wanted to break out each word so you can deal with exceptions, you could do something like this:
$separator = array(" ","-","+","'");
foreach($separator as $s)
{
if (strpos($name_formatted, $s) !== false)
{
$word = explode($s, $name_formatted);
$tmp_ary = array_map("ucfirst", array_map("strtolower", $word)); // whatever processing you want to do here
$name_formatted = implode($s, $tmp_ary);
}
}
return $name_formatted;
}
echo format_name($name_unformatted);
?>
您可以扩展此功能以处理您的姓名异常。
对于意大利名字的问题,我找到了一个简单的解决方案
function formatName(string $firstName, string $lastName): array
{
$delimiters = " -’'\t\r\n\f\v";
return \array_map(
fn ($string) => \ucwords(\mb_strtolower($string), $delimiters),
[$firstName, $lastName]
);
}
就我而言,我必须处理像D'Amico
,和这样的姓氏De Angelis
,而且这个快速的解决方案效果很好。Di Cataldo
Rossi-Bianchi
主要玩家是
\ucwords(\mb_strtolower($string), $delimiters)
其余的只是我通过划分名字和姓氏来处理名字的一部分。