将首字母大写,其余小写成字符串 [英] Make first letter uppercase and the rest lowercase in a string

查看:92
本文介绍了将首字母大写,其余小写成字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部, 我正在尝试将姓氏插入数据库.我希望首字母大写,如果他们使用两个姓氏,则将大写的名字和第二个字母大写.例如,如果有人输入:

All, I'm trying to insert a last name into a database. I'd like the first letter to be capitalized for the name and if they have use two last names then capitalize the first and second names. So for example if someone enters:

已婚姓氏

如果有两个以上的名字,它将把它转换为已婚名字"或"Maidenname",依此类推.另一种情况是某人的名字中带有撇号,因此如果有人输入,还是可以这样做:

It would convert this to Marriedname Maidenname and so on if there is more then two names. The other scenario is is someone has an apostrophe in their name, so is there anyway to do it if someone enters:

o'connell

o'connell

这需要转换为O'Connell.我正在使用:

This would need to convert to O'Connell. I was using:

ucfirst(strtolower($last_name));

但是,正如您所知,这不适用于所有情况.感谢您的任何建议!

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

推荐答案

这将大写所有单词的第一个字母,以及在单引号后的字母.它将使所有其他字母变为小写.它应该为您工作:

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天全站免登陆