PHP 中使用自定义分隔符将单词大写

发布于 2024-08-24 14:27:13 字数 971 浏览 3 评论 0原文

我需要一种方法将单词的每个第一个字母大写。 这就是我到目前为止所得到的,它几乎适用于每个字符串......但它在这个“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仄言 2024-08-31 14:27:13

我对你到底想做什么感到困惑。 PHP 已经有一个 ucwords() 函数可以做到这一点,但我不知道没有看到你正在做的事情有什么不同...如果你将每个单词的第一个字母大写,分隔符有什么区别吗?有一个“&”有那么重要吗?两个词之间?

编辑:我想我现在明白了。我认为您遇到的唯一问题是您无法判断它是大写文本,因为它已经全部大写,您需要做的就是先将其小写。我还改变了它以完全摆脱“下一个角色”的东西,这是不必要的。如果找到匹配项,只需将下一个字符更改为大写即可。试试这个:

// method in stringModify Class
function capitalizeWords($words, $charList) {
    $words = strtolower($words); // lowercase everything that isn't capitalized
    for ($i = 0; $i < strlen($words); $i++) {
        if (strpos($charList, $words[$i]) !== false) $words[$i+1] = strtoupper($words[$i+1]);
    }
    return $words;
}
// Calling method
$stringModify->capitalizeWords("WELLNESS & RENOMME", " -&");

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:

// method in stringModify Class
function capitalizeWords($words, $charList) {
    $words = strtolower($words); // lowercase everything that isn't capitalized
    for ($i = 0; $i < strlen($words); $i++) {
        if (strpos($charList, $words[$i]) !== false) $words[$i+1] = strtoupper($words[$i+1]);
    }
    return $words;
}
// Calling method
$stringModify->capitalizeWords("WELLNESS & RENOMME", " -&");
祁梦 2024-08-31 14:27:13
function cb($word){ return ucwords(strtolower($word[0])); }
var_dump(preg_replace_callback('@[A-z]+@i','cb','TESTING-TESTING / TESTING & TEST / testing*&^123!&%&*TEST'));
function cb($word){ return ucwords(strtolower($word[0])); }
var_dump(preg_replace_callback('@[A-z]+@i','cb','TESTING-TESTING / TESTING & TEST / testing*&^123!&%&*TEST'));
最佳男配角 2024-08-31 14:27:13

要转换所有句子中的第一个字母大写,请使用以下代码。

前任)

$string = strtolower($string);
echo preg_replace('/(^|[\.!?]"?\s+)([a-z])/e', '"$1" . ucfirst("$2")', $string); 
?>

For converting the first letter capital in all sentence use the below code.

Ex)

$string = strtolower($string);
echo preg_replace('/(^|[\.!?]"?\s+)([a-z])/e', '"$1" . ucfirst("$2")', $string); 
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文