使所有单词小写,每个单词的第一个字母大写 [英] Make all words lowercase and the first letter of each word uppercase

查看:39
本文介绍了使所有单词小写,每个单词的第一个字母大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大小写不正确的字符串,如下所示:

I have a string with improper capitalization scattered like below:

$str = "tHis iS a StRinG thAt NeEds ProPer CapiTilization";
$newStr = ucfirst($str);
echo $newStr;

我如何能够将每个单词的第一个字母大写,而将错误的大写字母小写?我需要字符串完全是标题大小写.

How would I be able to capitalize the first letter of each word and lower case the incorrectly capitalized letters? I need the string to be fully title case.

我知道我可以更改为降低然后使用 ucwords() 但有没有更短的方法来做到这一点?

I know I can change to lower and then use ucwords() but is there a shorter way to do this?

推荐答案

如何将每个单词的首字母大写,将错误的大写字母小写?

How would I be able to capitalize the first letter of each word and lower case the incorrectly capitalized letters?

ucwords() 将首字母大写每个单词的字母.您可以将它与 strtolower() 结合起来使用全部小写.

ucwords() will capitilize the first letter of each word. You can combine it with strtolower() to first lowercase everything.

例如:

ucwords(strtolower('HELLO WORLD!')); // Hello World!

这篇关于使所有单词小写,每个单词的第一个字母大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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