ucwords function with exceptions

前端 未结 5 2247
你的背包
你的背包 2020-12-17 05:47

I need some help i have this code that Uppercase the first character of each word in a string with exceptions i need the function to ignore the exception if it\'s at the beg

5条回答
  •  醉酒成梦
    2020-12-17 06:21

    Do this really cheaply by just always uppercasing your first word:

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

提交回复
热议问题