如何在PHP中使第一个单词的第一个字符小写?

如何在PHP中使第一个单词的第一个字符小写?,php,lowercase,Php,Lowercase,如何将其转换为: "Elephant, Africa, landscape" 为此: "elephant, Africa, landscape" 我尝试使用和php函数,但这不是我想要的。我希望第一个单词的第一个字符是小写的,而不是所有的句子都是小写的,或者所有的单词都是第一个字符 有什么东西可以让第一个单词的第一个字符只用小写字母吗 更新: 我希望将文章标题显示为关键字,我使用以下内容: $title = get_the_title( $post->post_parent ); //

如何将其转换为:

"Elephant, Africa, landscape"
为此:

"elephant, Africa, landscape"
我尝试使用和php函数,但这不是我想要的。我希望第一个单词的第一个字符是小写的,而不是所有的句子都是小写的,或者所有的单词都是第一个字符

有什么东西可以让第一个单词的第一个字符只用小写字母吗

更新:

我希望将文章标题显示为关键字,我使用以下内容:

$title = get_the_title( $post->post_parent ); // Get post title
$parts = explode( ' ', $title ); // Delimetar for words in post title " "
$str = '';
foreach ($parts as $word) {
    $str.= lcfirst(post_title_as_keywords($word)); // Lowercase first character
}
$str = substr($str, 0,-2);
echo $str;
这就是我得到的:

大象、非洲、风景

如何防止所有单词出现小写效果

PHP版本:>5.3.0:

函数的作用是:将字符串的第一个字符转换为小写

对于PHP<5.3,请使用

$your_string = "Elephant, Africa, landscape";
$your_string[0] = strtolower($str[0]);

//output elephant, Africa, landscape
更新:

使用foreach循环之外的函数将解决问题

PHP版本:>5.3.0:

函数的作用是:将字符串的第一个字符转换为小写

对于PHP<5.3,请使用

$your_string = "Elephant, Africa, landscape";
$your_string[0] = strtolower($str[0]);

//output elephant, Africa, landscape
更新:

使用foreach循环之外的函数将解决问题

您正在使用
lcfirst()
每个单词一次(在
for
循环中)。尝试将
lcfirst()

$str = lcfirst(substr($str, 0,-2));
每个单词使用一次
lcfirst()
(在
for
循环中)。尝试将
lcfirst()

$str = lcfirst(substr($str, 0,-2));

lcfirst()
怎么没有按你的意愿去做?是因为缺少unicode吗。lcfirst正是你想要的。请阅读文档并试一试:lcfirst为您做了哪些您不喜欢的事情?通过lcfirst,我得到了“大象,非洲,风景”。这是我在控制台上运行lcfirst()时得到的结果:php>echo lcfirst(“大象,非洲,风景”);大象,非洲,请检查您的代码/输入字符串,该函数工作正常。
lcfirst()
怎么没有按您的意愿执行?是因为缺少unicode吗。lcfirst正是你想要的。请阅读文档并试一试:lcfirst为您做了哪些您不喜欢的事情?通过lcfirst,我得到了“大象,非洲,风景”。这是我在控制台上运行lcfirst()时得到的结果:php>echo lcfirst(“大象,非洲,风景”);大象,非洲,风景检查您的代码/输入字符串,该函数工作正常。