欢迎访问宙启技术站
智能推送

PHPucwords()函数详解及其示例

发布时间:2023-06-06 10:52:53

PHPucwords()函数是用来将字符串中的单词首字母大写的PHP函数。

语法:ucwords(string $str, string $delimiters = " \t\r

\f\v")

参数:

$str:必需。要进行首字母大写的字符串。

$delimiters:可选。指定分隔符字符串,其内部存储分隔符。默认情况下,“ \t\r

\f\v” 五个字符都被包括进去。

示例1:使用默认分隔符

$txt = "hello world!";
echo ucwords($txt);

输出:

Hello World!

示例2:指定分隔符

$txt = "hello-world!";
echo ucwords($txt, "-");

输出:

Hello-World!

示例3:过滤空格和换行符

$txt = "hello
\tworld!";
echo ucwords($txt, " 
\t");

输出:

Hello
    World!

提示:ucwords()只能处理ASCII字符,对于Unicode字符则需要加上mb_ucwords()函数来实现。