gpt4 book ai didi

php - ucwords 函数有异常

转载 作者:可可西里 更新时间:2023-11-01 00:15:37 27 4
gpt4 key购买 nike

我需要一些帮助我有这段代码,大写字符串中每个单词的第一个字符,但有异常(exception)我需要函数来忽略异常,如果它在字符串的开头:

function ucwordss($str, $exceptions) {
$out = "";
foreach (explode(" ", $str) as $word) {
$out .= (!in_array($word, $exceptions)) ? strtoupper($word{0}) . substr($word, 1) . " " : $word . " ";
}
return rtrim($out);
}

$string = "my cat is going to the vet";
$ignore = array("is", "to", "the");
echo ucwordss($string, $ignore);
// Prints: My Cat is Going to the Vet

这就是我正在做的:

$string = "my cat is going to the vet";
$ignore = array("my", "is", "to", "the");
echo ucwordss($string, $ignore);
// Prints: my Cat is Going to the Vet
// NEED TO PRINT: My Cat is Going to the Vet

最佳答案

- return rtrim($out);
+ return ucfirst(rtrim($out));

关于php - ucwords 函数有异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11996446/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com