我需要一些帮助我有这段代码,大写字符串中每个单词的第一个字符,但有异常(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/