How to capitalize words with brackets in sentence

前端 未结 1 1921
Happy的楠姐
Happy的楠姐 2021-01-14 03:34

I am using the following code to capitalize each word in a sentence, but I am unable to capitalize words with brackets attached.

PHP Code:

  

        
相关标签:
1条回答
  • 2021-01-14 04:33
    $output = ucwords($str, ' [{(');
    echo $output;
    // output ->
    // [This Is The {Command Line (Interface
    

    Update: general solution. Here a "bracket" - is any non-letter character. Any letter, following the "bracket" is converted to uppercase.

    $string = "test is the {COMMAND line -STRET (interface 5more 9words #here";
    $strlowercase = strtolower($string);
    
    $result = preg_replace_callback('~(^|[^a-zA-Z])([a-z])~', function($matches)
    {
        return $matches[1] . ucfirst($matches[2]);
    }, $strlowercase);
    
    
    var_dump($result);
    // string(62) "Test Is The {Command Line -Stret (Interface 5More 9Words #Here"
    

    Live demo

    0 讨论(0)
提交回复
热议问题