Php headword after parenthesis

I am trying to use every word in a php line, but the function does not detect a word immediately followed by a bracket. How can I make it so that the word immediately after the bracket is capitalized?

Example: Amharic (Ethiopian) ... Amharic (Ethiopian)

(currently using ucwords (), PHP displays Amharic (Ethiopian))

+5
source share
6 answers

This is a known bug that requires a space between the open bracket and the first letter. Here is a workaround:

$var = "amharic (ethiopian)";

echo str_replace('( ', '(', ucwords(str_replace('(', '( ', $var)));

Result

Amharic (Ethiopian)

See demo

+16
source

Try it, I tried

$text= "amharic (ethiopian)";
echo mb_convert_case($text, MB_CASE_TITLE, "UTF-8");

output : Amharic (Ethiopian)

. , mbstring PHP .

+7

    function ucWordWithBracket($edit){
        $new_word = str_replace("(","( ",$edit);
        $temp = ucwords($new_word);
        $new_word = str_replace("( ","(",$temp);
        return $new_word;
    }

ucWordWithBracket ( ())

- " ()";

+1

preg_replace (http://php.net/manual/en/function.preg-replace.php) /[A-Z][a-zA-Z]*/ .

0

, , php , , , folow

, , , , php , :)

0

For everyone else that needs a heading, around other characters, including dashes, brackets, and parentheses. You can use the regular expression with preg_replace_callback to capture a word that is broken by a dash or begins with a character that does not contain an alphabet.

/** Capitalize first letter if string is only one word **/
    $STR = ucwords(strtolower($STR));

/** Correct Title Case for words with non-alphabet charcters ex. Screw-Washer **/
    $STR = preg_replace_callback('/([A-Z]*)([^A-Z]+)([A-Z]+)/i', 
        function ($matches) {
            $return = ucwords(strtolower($matches[1])).$matches[2].ucwords(strtolower($matches[3]));
            return $return;
        }, $STR);
0
source

All Articles