标题大小写包含一个或多个姓氏的字符串,同时处理带有撇号的姓名 [英] Title case a string containing one or more last names while handling names with apostrophes

查看:27
本文介绍了标题大小写包含一个或多个姓氏的字符串,同时处理带有撇号的姓名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想标准化用户提供的字符串.我希望名字的第一个字母大写,如果他们输入了两个姓氏,则将名字和第二个名字大写.例如,如果有人输入:

I want to standardize a user-supplied string. I'd like the first letter to be capitalized for the name and if they have entered two last names, then capitalize the first and second names. For example, if someone enters:

marriedname maidenname

如果有两个以上的名字,它会将其转换为 Marriedname Maidenname 等等.

It would convert this to Marriedname Maidenname and so on if there is more than two names.

另一种情况是某人的名字中有撇号.如果有人进入:

The other scenario is when someone has an apostrophe in their name. If someone enters:

o'connell

这需要转换为O'Connell.

我正在使用:

ucfirst(strtolower($last_name));

但是,正如您所知,这不适用于所有场景.

However, as you can tell that wouldn't work for all the scenarios.

推荐答案

这将大写所有单词的第一个字母,以及紧跟在撇号之后的字母.它将使所有其他字母小写.它应该适合你:

This will capitalize all word's first letters, and letters immediately after an apostrophe. It will make all other letters lowercase. It should work for you:

str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($last_name))));

这篇关于标题大小写包含一个或多个姓氏的字符串,同时处理带有撇号的姓名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆