Proper capitalization of surnames in PHP [duplicate]

孤人 提交于 2019-12-14 03:48:49

问题


Possible Duplicate:
Capitalization of Person names in programming

I've noticed that people who are registering on my site are exceptionally lazy in the way that they don't even bother capitalizing their own names.

My site is a business oriented one, so there's no "freedom of self-expression" argument here.

First name capitalization is pretty easy as I can't think of a single instance where a Western first name would not start with a capital letter. I could be wrong.

But capitalizing the last name gets more difficult, with names like

O'Brien
O´Flaherty
de Wit
McKenzie
Auditore da Firenze
de los Remedios de Escalada
Virta-Jokela

What would be a good solution to proper automatic capitalization of surnames in PHP that would get it right some 95% of the time? I am aware of this.


回答1:


Here's a quick and dirty solution off the top:

  1. Split the string into words separated by whitespace and dash
  2. For each word:
    • If it's inside a fixed list of stop words ("de", "los", etc), do not modify.
    • If not, check if it has a prefix in a fixed list (this list would contain things like "O'", "Mc", etc). If such a prefix exists then normalize it (e.g. translating O" to O') and move to the next step considering the word without the prefix.
    • Uppercase the first letter of the word.



回答2:


At first it seems like an easy job: simply capitalize every word you encounter in the last name. So foo bar will become Foo Bar.

However, as you already pointed out, there are exceptions:

  • de Wit
  • Auditore da Firenze
  • de los Remedios

This can be solved with a blacklist of fragments you don't want capitalized ('de', 'da', 'de los' given this example). But then you falsely assume that 'De', 'Da' and 'De Los' do not exist as (parts of) last names that should be capitalized.

So simply said: no, this can't be done good, only half-wittedly.




回答3:


It make the first character of each letter capital $name = ucwords(strtolower($name));

This will be accurate for more than 95% cases.

It is not clear, how do you want de los Remedios de Escalada to be ? With above expression it will become De Los Remedios De Escalada. I am notsure if it is desired.

Another way is to explode the name by " " and make the first character of Last word capital using ucword()



来源:https://stackoverflow.com/questions/10580554/proper-capitalization-of-surnames-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!