PHP字符串转换第一个字符大小写

PHP字符串转换第一个字符大小写,php,uppercase,lowercase,Php,Uppercase,Lowercase,我有两种类型的字符串,“hello”,“hellother”。 我想要的是改变它们,使它们看起来像:“你好”,“你好”,这取决于具体情况 做这件事的好方法是什么 谢谢使用以下功能: echo ucwords('hello world'); 返回包含第一个字符串的字符串 str中每个单词的字符 大写,如果该字符为 按字母顺序 单词的定义是任何字符串 指立即被删除的字符 在空白之后(这些是:空格, 换行、换行、回车、, 水平选项卡和垂直选项卡) 这将不会分割拼凑在一起的单词-您必须根据需要在字符串

我有两种类型的字符串,
“hello”,“hellother”。

我想要的是改变它们,使它们看起来像:
“你好”,“你好”,这取决于具体情况

做这件事的好方法是什么

谢谢

使用以下功能:

echo ucwords('hello world');
返回包含第一个字符串的字符串 str中每个单词的字符 大写,如果该字符为 按字母顺序

单词的定义是任何字符串 指立即被删除的字符 在空白之后(这些是:空格, 换行、换行、回车、, 水平选项卡和垂直选项卡)


这将不会分割拼凑在一起的单词-您必须根据需要在字符串中添加空格,此函数才能工作。

PHP有许多字符串操作函数
ucfirst()
将为您完成此操作


使用
ucwords
功能:

echo ucwords('hello world');

要将大小写转换为不同的单词:

preg_replace('/([^A-Z])([A-Z])/', "$1 $2", $string)
将所有单词的首字母大写:

ucwords()
因此:

ucwords(preg_replace('/([^A-Z])([A-Z])/', "$1 $2", $string))

你可以像大家说的那样使用ucwords。。。要在
hellother
中添加空格,您可以使用_space=preg_replace('/[A-Z]/',“$0”,$string)执行
$
then
ucwords($with_空格)

使用ucwords

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

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

要使舒尔it在其他语言上工作,UTF-8可能是一个好主意。我在wordpress安装中对任何语言都使用防水功能

$str = mb_ucfirst($str, 'UTF-8', true);
这使得第一个字母大写,其他所有字母小写。如果第三个参数设置为false(默认值),则不会操作字符串的其余部分。然而,这里有人可能会建议重新使用函数本身,并在第一个单词之后使用mb大写字母,以便更准确地回答这个问题

// Extends PHP
if (!function_exists('mb_ucfirst')) {

function mb_ucfirst($str, $encoding = "UTF-8", $lower_str_end = false) {
    $first_letter = mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding);
    $str_end = "";
    if ($lower_str_end) {
        $str_end = mb_strtolower(mb_substr($str, 1, mb_strlen($str, $encoding), $encoding), $encoding);
    } else {
        $str_end = mb_substr($str, 1, mb_strlen($str, $encoding), $encoding);
    }
    $str = $first_letter . $str_end;
    return $str;
}

}

/Lundman

他需要先用
helloThere
添加空格。不,该函数不能将CamelCase改为CamelCase。我想你有一本字典来确定复合词的拆分位置?!