PHP 中使用自定义分隔符将单词大写
我需要一种方法将单词的每个第一个字母大写。 这就是我到目前为止所得到的,它几乎适用于每个字符串......但它在这个“WELLNESS & RENOMME”上失败了。
// method in stringModify Class
function capitalizeWords($words, $charList) {
$capitalizeNext = true;
for ($i = 0, $max = strlen($words); $i < $max; $i++) {
if (strpos($charList, $words[$i]) !== false) {
$`capitalizeNext` = true;
} else if ($capitalizeNext) {
$capitalizeNext = false;
$words[$i] = strtoupper($words[$i]);
}
}
return $words;
}
// Calling method
$stringModify->capitalizeWords("WELLNESS & RENOMME", " -&");
我希望有人能帮助我......我已经尝试了 1.5 个小时,但没有任何线索。预先感谢您提供任何提示或提示。
编辑
ucwords() 使用“”作为分隔符,我也想使用“-”。
编辑
感谢大家为您提供的解决方案。我现在要去睡觉了,现在是早上 7 点。 :D 当我醒来时,我会看看我最喜欢哪种解决方案,然后告诉你我选择了哪一种。
编辑
似乎所有功能都返回“wellness &Renomme”或“wellness & Renomme”。我的 php.ini 中的某些内容是否可能被搞乱了?
I need a method to capitalize every first letter of a word.
This is what i got so far and it is working for almost every string...but it fails on this one "WELLNESS & RENOMME".
// method in stringModify Class
function capitalizeWords($words, $charList) {
$capitalizeNext = true;
for ($i = 0, $max = strlen($words); $i < $max; $i++) {
if (strpos($charList, $words[$i]) !== false) {
I need a method to capitalize every first letter of a word.
This is what i got so far and it is working for almost every string...but it fails on this one "WELLNESS & RENOMME".
capitalizeNext` = true;
} else if ($capitalizeNext) {
$capitalizeNext = false;
$words[$i] = strtoupper($words[$i]);
}
}
return $words;
}
// Calling method
$stringModify->capitalizeWords("WELLNESS & RENOMME", " -&");
I hope someone can help me out...i tried for 1,5 hours now and don't have a clue. Thanks in advance for any tips or hints.
edit
ucwords() uses " " as delimitor and i want to use "-" for example too.
edit
thanks to you all for you solutions. i will go to bed now, its 7 in the morning here. :D i will see which solution i like best when i wake up and then tell you which one i choosed.
edit
it seems like all the functions are returning "wellness &Amp; Renomme" or "wellness & Renomme". is it possible that something in my php.ini is messed up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对你到底想做什么感到困惑。 PHP 已经有一个
ucwords()
函数可以做到这一点,但我不知道没有看到你正在做的事情有什么不同...如果你将每个单词的第一个字母大写,分隔符有什么区别吗?有一个“&”有那么重要吗?两个词之间?编辑:我想我现在明白了。我认为您遇到的唯一问题是您无法判断它是大写文本,因为它已经全部大写,您需要做的就是先将其小写。我还改变了它以完全摆脱“下一个角色”的东西,这是不必要的。如果找到匹配项,只需将下一个字符更改为大写即可。试试这个:
I'm confused at what exactly you're trying to do. PHP already has a
ucwords()
function that can do this and I don't see any difference in what you're doing... If you're capitalizing the first letter of each word, do delimiters make any difference? Does it matter at all that there's a '&' between the two words?Edit: I think I understand now. I assume the only problem you're having is that you can't tell it's uppercasing the text because it's already all uppercased, all you need to do is lowercase it first. I also changed it to completely get rid of the 'next character' thing, it was unnecessary. If you find a match, just change the next character to uppercase. Try this:
要转换所有句子中的第一个字母大写,请使用以下代码。
前任)
For converting the first letter capital in all sentence use the below code.
Ex)