问题描述
我得到了一个大写的名字列表.为了在电子邮件中表达敬意,我希望他们能够妥善处理.
I am provided a list of names in upper case. For the purpose of a salutation in an email I would like them them to be Proper Cased.
使用 PHP 的 ucwords 很容易做到.但是我觉得我需要一些正则表达式函数来处理常见的异常,比如:
Easy enough to do using PHP's ucwords. But I feel I need some regex function to handle common exceptions, such as:
奥哈拉"、麦当劳"、范德斯洛特"等
我不需要帮助构建一个正则表达式来处理上面的三个示例(虽然这很好),因为我不知道所有常见的异常可能是什么.
It's not so much that I need help constructing a regex statement to handle the three examples above (tho that would be nice), as it is that I don't know what all the common exceptions might be.
肯定有人以前遇到过这个问题,是否有任何指向已发布解决方案的指针或您可以分享的内容?
Surely someone has faced this issue before, any pointers to published solutions or something you could share?
推荐答案
在提供的简短列表中使用正则表达式可能很容易,但如果您必须处理成百上千条记录,则很难做到万无一失.
Using regular expressions in a short provided list could be easy, but if you must handle hundreds or thousands of records it's very hard to be bullet proof.
我宁愿使用不会影响其他人的东西.你怎么知道麦克唐纳"先生是否更喜欢麦克唐纳"?
I'd rather use something that can't affect someone else. How do you know if Mr. "MACDONALD" prefers "Macdonald"?
您正在纠正别人的错误.如果无法更正来源,您可以使用以下方法:
You're correcting someone else's error. If source cannot be corrected you could use something like this:
<?php
$provided_names = array(
"SMITH",
"O'HARA",
"MCDONALD",
"JONES",
"VAN DER SLOOT",
"MACDONALD"
);
$corrected_names = array(
"O'HARA" => "O'Hara",
"MCDONALD" => "McDonald",
"VAN DER SLOOT" => "van der Sloot"
);
$email_text = array();
foreach ($provided_names as $provided_name)
{
$provided_name = !array_key_exists($provided_name, $corrected_names)
? ucwords(strtolower($provided_name))
: $corrected_names[$provided_name];
$email_text[] = "{$provided_name}, your message text.";
}
print_r($email_text);
/* output:
Array
(
[0] => Smith, your message text.
[1] => O'Hara, your message text.
[2] => McDonald, your message text.
[3] => Jones, your message text.
[4] => van der Sloot, your message text.
[5] => Macdonald, your message text.
)
*/
?>
希望有用.
这篇关于给定大写名称转换为正确大小写,处理“O'Hara"、“McDonald"和“McDonald".《范德斯洛特》等等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!