Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP输出中每个单词的第一个大写字母_Php_Capitalization - Fatal编程技术网

PHP输出中每个单词的第一个大写字母

PHP输出中每个单词的第一个大写字母,php,capitalization,Php,Capitalization,我希望输出的每个单词的第一个字母都是大写的。这是我的密码 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; ret

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

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);
谢谢您的帮助。

只处理单个字符串,而不是数组。只需在选择随机标题后应用它即可:

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!
}

您需要使用ucfirst以大写字母书写第一个字母。您需要在返回时使用它,我认为基于
return$quotes1[$num]
$quotes1
是一个数组,但
ucwords()
用于字符串-。您可以执行
返回ucwords($quotes1[$num])这是完美的。它成功了。非常感谢。但是应该把所有的信都封起来吗?是吗?@anantkumarsingh
ucwords
将每个单词的第一个字符大写。还是我误解了你的评论?是的,上面的代码输出为:这是你的输出