php - 使所有单词小写,每个单词的第一个字母大写

标签 php string title-case ucfirst

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

$str = "tHis iS a StRinG thAt NeEds ProPer CapiTilization";
$newStr = ucfirst($str);
echo $newStr;
我如何能够将每个单词的第一个字母大写,并将错误的大写字母小写?我需要字符串完全是标题大小写。
我知道我可以更改为降低然后使用 ucwords()但是有没有更短的方法来做到这一点?

最佳答案

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

ucwords() 将大写每个单词的第一个字母。您可以将其与 strtolower() 结合使用首先小写所有内容。
例如:
ucwords(strtolower('HELLO WORLD!')); // Hello World!

关于php - 使所有单词小写,每个单词的第一个字母大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32564539/

相关文章:

c++ - 将字符串拆分为标记 - 没有操作系统特定的功能

string - 使用 PowerShell 拆分字符串并对每个 token 执行某些操作

string - 将字符串转换为首字母大写 - Emacs Lisp

php - Laravel 5.4 连接两个表

php - 通过 "google-api-php-client"库检索 Google 搜索分析

If Else 语句响应中的 JavaScript For 循环

php - MySQL UTF8 字符加载

c++ - std::string 上下文中首字母缩写词 SSO 的含义

Python:除了以数字开头的字符串之外,是否有单行脚本来标题大小写字符串?

r - 将双字字符串中两个单词的第一个字母大写