如何将PHP中第一个单词的第一个字符转为小写?
How to make the first character of first word lowercase in PHP?
如何转换:
"Elephant, Africa, landscape"
进入这个:
"elephant, Africa, landscape"
我尝试使用 strlower and lcfirst php 函数,但这不是我想要的。我希望只有第一个单词的第一个字符是小写的,而不是所有句子小写或所有单词的第一个字符小写。
有什么东西可以让第一个单词的第一个字符只小写吗?
更新:
我希望显示 post 标题作为关键字,我使用这个:
$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;
这就是我得到的:
"elephant, africa, landscape"
如何防止所有单词出现小写效果?
PHP 版本:> 5.3.0:
lcfirst() 函数将字符串的第一个字符转换为小写。
echo lcfirst("Elephant, Africa, landscape");
//output elephant, Africa, landscape
对于 PHP < 5.3 使用
$your_string = "Elephant, Africa, landscape";
$your_string[0] = strtolower($str[0]);
//output elephant, Africa, landscape
更新:
使用不在 foreach 循环内的函数将解决您的问题。
您每个单词使用了一次 lcfirst()
(在 for
循环内)。尝试将 lcfirst()
应用到 str 在 循环之后,而不是在循环内部:
$str = lcfirst(substr($str, 0,-2));
如何转换:
"Elephant, Africa, landscape"
进入这个:
"elephant, Africa, landscape"
我尝试使用 strlower and lcfirst php 函数,但这不是我想要的。我希望只有第一个单词的第一个字符是小写的,而不是所有句子小写或所有单词的第一个字符小写。
有什么东西可以让第一个单词的第一个字符只小写吗?
更新:
我希望显示 post 标题作为关键字,我使用这个:
$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;
这就是我得到的:
"elephant, africa, landscape"
如何防止所有单词出现小写效果?
PHP 版本:> 5.3.0:
lcfirst() 函数将字符串的第一个字符转换为小写。
echo lcfirst("Elephant, Africa, landscape");
//output elephant, Africa, landscape
对于 PHP < 5.3 使用
$your_string = "Elephant, Africa, landscape";
$your_string[0] = strtolower($str[0]);
//output elephant, Africa, landscape
更新:
使用不在 foreach 循环内的函数将解决您的问题。
您每个单词使用了一次 lcfirst()
(在 for
循环内)。尝试将 lcfirst()
应用到 str 在 循环之后,而不是在循环内部:
$str = lcfirst(substr($str, 0,-2));