gpt4 book ai didi

php - 使用 preg_replace 将单词的首字母大写

转载 作者:行者123 更新时间:2023-12-04 15:31:47 24 4
gpt4 key购买 nike

我需要将始终为小写的名称转换为大写。

例如约翰·约翰逊 -> 约翰·约翰森

还有:

强尼巴特约翰逊 -> 强尼巴特约翰森

我如何使用 PHP 完成此操作?

最佳答案

你也可以使用正则表达式:

preg_replace_callback('/\b\p{Ll}/', 'callback', $str)

\b 表示单词边界,\p{Ll} 描述 Unicode 中的任何小写字母。 preg_replace_callback将为每个匹配项调用一个名为 callback 的函数,并用其返回值替换匹配项:

function callback($match) {
return mb_strtoupper($match[0]);
}

在这里mb_strtoupper用于将匹配的小写字母转换为大写。

关于php - 使用 preg_replace 将单词的首字母大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3450244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com