php - ucwords 函数有异常

标签 php

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

相关文章:

php - 无法从列中选择最高值

php - 更新失败 : Select Command Denied for User (vahejaba @ localhost)

php - 如何在 PHP 中从 cPanel 获取磁盘空间使用情况和每月带宽传输到我的网站

php共享内存读取全0

javascript - 图片上传ajax jquery

php - 登录脚本问题,不显示 "Logged in"

php - 向仅包含数字的日期字符串添加斜杠

php - 如何使用 .htaccess 重写带有两个变量的 url

php - 获取多个输入的值

php - 将 .htaccess 与 FastCGI 结合使用