How to uppercase first letter after a hyphen, ie Adam Smith-Jones

前端 未结 9 1473
野趣味
野趣味 2021-02-01 22:30

I\'m looking for a way to uppercase the first letter/s of a string, including where the names are joined by a hyphen, such as adam smith-jones needs to be Adam Smith-Jones.

相关标签:
9条回答
  • 2021-02-01 23:29

    You can us 'ucwords' to capitalize all words at once, and 'implode' and 'explode' together, like this:

    ucwords(implode(" ", explode("_", "my_concatinated_word_string")));
    
    0 讨论(0)
  • 2021-02-01 23:29

    Here is a simple function that can convert all the words in a string to title case:

    function toTitleCase($string) {
        return preg_replace_callback('/\w+/', function ($match) {
            return ucfirst(strtolower($match[0]));
        }, $string);
    }
    
    0 讨论(0)
  • 2021-02-01 23:30
    /**
    * Uppercase words including after a hyphen
    *
    * @param string $text lower-case text
    * @return string Upper-Case text
    */
    function uc_hyphenated_words($text)
    {
        return str_replace("- ","-",ucwords(str_replace("-","- ",$text)));
    }
    
    0 讨论(0)
提交回复
热议问题