Ucwords not uppercase with accent

I have a string with all the letters uppercase. I use the functions ucwords () and mb_strtolower () to use only the first letter of the string. But I have problems when there is an accent in the first letter of a word. For instance:

ucwords(mb_strtolower('GRANDE ÁRVORE')); //outputs 'Grande árvore' 

Why is the first letter of the second word not capitalized? What can I do to solve this problem?

+6
source share
2 answers

ucwords is one of the core functions of PHP that blissfully ucwords encodings other than ASCII or non-Latin-1. * To process multibyte strings and / or non-ASCII strings, you should use multibyte mb_convert_case :

 mb_convert_case($str, MB_CASE_TITLE, 'UTF-8') // your string encoding here --------^^^^^^^ 

* I'm not quite sure if it works only with ASCII or, at least, with Latin-1, but I wouldn't even figure it out.

+8
source

ucwords does not recognize accented characters. Try using mb_convert_case .

 $str = 'GRANDE ÁRVORE'; function ucwords_accent($string) { if (mb_detect_encoding($string) != 'UTF-8') { $string = mb_convert_case(utf8_encode($string), MB_CASE_TITLE, 'UTF-8'); } else { $string = mb_convert_case($string, MB_CASE_TITLE, 'UTF-8'); } return $string; } echo ucwords_accent($str); 
-1
source

All Articles