【问题标题】:PHP First Letter Uppercase of Each Word In Output [duplicate]输出中每个单词的PHP首字母大写[重复]
【发布时间】:2015-05-04 21:20:59
【问题描述】:

我希望输出的每个单词的第一个字母大写。这是我的代码。

function random_title () 
{ 
    $quotes1 = file ("wp-content/plugins/includes/classes/quotes.txt", FILE_IGNORE_NEW_LINES);
    $quotes1 = ucwords($quotes1);
    $num = rand (0, intval (count ($quotes1) / 3)) * 3;
    return $quotes1[$num];
}

用法是:

random_title()

这部分不起作用,我做错了什么?当我把它放进去时,我没有得到任何输出,但如果我把它拿出来,我会得到我的标题,但它们是小写的,因为它们在文本文件中。

    $quotes1 = ucwords($quotes1);

感谢您的帮助。

【问题讨论】:

  • 您需要使用 ucfirst 将首字母大写。我认为您需要在返回时使用它
  • 基于return $quotes1[$num];$quotes1 是一个数组,但ucwords() 将用于字符串-string ucwords ( string $str ) 。你可以做return ucwords($quotes1[$num]);

标签: php capitalization


【解决方案1】:

ucwords 适用于单个字符串,而不是数组。只需选择一个随机标题后应用它:

function random_title () 
{ 
    $quotes1 = file ("wp-content/plugins/includes/classes/quotes.txt", FILE_IGNORE_NEW_LINES);
    $num = rand (0, intval (count ($quotes1) / 3)) * 3;
    return ucwords($quotes1[$num]); # Here!
}

【讨论】:

  • 但它应该大写所有字母?不是吗?
  • @anantkumarsingh ucwords 将每个单词的第一个字符大写。还是我误解了你的评论?
  • 是的,上面的代码输出为:This Is Your Output
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-30
  • 2016-12-05
  • 2015-11-10
  • 2017-08-02
  • 2013-12-15
  • 1970-01-01
相关资源
最近更新 更多