Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ucwords not capitalizing accented letters

Tags:

php

I have a string with all letters capitalized. I'm using the ucwords() and the mb_strtolower() functions to capitalize only the first letter of a string. But I'm having some problems when the first letter of a word have a accent. For example:

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

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

like image 807
Heverton Coneglian de Freitas Avatar asked May 13 '14 12:05

Heverton Coneglian de Freitas


1 Answers

ucwords is one of the core PHP functions which is blissfully oblivious to non-ASCII or non-Latin-1 encodings.* For handling multibyte strings and/or non-ASCII strings, you should use the multibyte aware mb_convert_case:

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

* I'm not entirely sure whether it works only with ASCII or at least with Latin-1, but I wouldn't even bother to find out.

like image 160
deceze Avatar answered Oct 20 '22 20:10

deceze