ucwords

(PHP 4, PHP 5, PHP 7, PHP 8)

ucwords将字符串中每个单词的首字母转换为大写

说明

ucwords(string $string, string $separators = " \t\r\n\f\v"): string

string 中每个单词的首字符(如果首字符是介于 "a"(0x61)和 "z"(0x7a)之间的 ASCII 字符)转换为大写字母,并返回这个字符串。

对于此函数,单词是未在 separators 参数列出的字符串。默认情况下,它们是:空格、水平制表符、回车、换行符、换页以及垂直制表符。

要对多字节进行类似的转换,请使用带 MB_CASE_TITLE 模式的 mb_convert_case()

参数

string

输入字符串。

separators

可选的 separators,包含了单词分割符。

返回值

返回转换后的字符串。

更新日志

版本 说明
8.2.0 大小写转换不在依赖于使用 setlocale() 设置的区域。只会转换 ASCII 字符。

范例

示例 #1 ucwords() 范例

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

$bar = 'HELLO WORLD!';
$bar = ucwords($bar); // HELLO WORLD!
$bar = ucwords(strtolower($bar)); // Hello World!
?>

示例 #2 ucwords() 自定义分隔符的例子

<?php
$foo
= 'hello|world!';
$bar = ucwords($foo); // Hello|world!

$baz = ucwords($foo, "|"); // Hello|World!
?>

示例 #3 带附加分隔符的 ucwords() 示例

<?php
$foo
= "mike o'hara";
$bar = ucwords($foo); // Mike O'hara

$baz = ucwords($foo, " \t\r\n\f\v'"); // Mike O'Hara
?>

注释

注意: 此函数可安全用于二进制对象。

参见

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top