在PHP中大写字符串的每个单词?

2 php string

据我所知,strtolower使字符串全部小写,ucfirst使字符串的第一个字母大写.

我问,是否有可能使字符串中的每个单词都大写?

示例$ string ="hello world" - 如何使其显示为"Hello World"?

Pao*_*ino 13

您正在寻找这个ucwords功能.直接来自PHP文档的示例:

$foo = 'hello world!';
$foo = ucwords($foo);             // Hello World!

$bar = 'HELLO WORLD!';
$bar = ucwords($bar);             // HELLO WORLD!
$bar = ucwords(strtolower($bar)); // Hello World!
Run Code Online (Sandbox Code Playgroud)


jer*_*ear 6

为了确保一致性,首先将整个字符串设置为小写是一个好习惯.

$foo = ucwords(strtolower($string));
Run Code Online (Sandbox Code Playgroud)