php – strtoupper() with accents

Question:

PHP's strtoupper() function is not capitalizing letters with accents, see example:

echo strtoupper("virá"); // retorna VIRá

Do you have any native functions that solve this problem?

Answer:

You need to use its counterpart, mb_strtoupper() which will handle unicode:

$encoding = mb_internal_encoding(); // ou UTF-8, ISO-8859-1...
echo mb_strtoupper("virá", $encoding); // retorna VIRÁ

Where

$encoding = 'UTF-8'; // ou ISO-8859-1...
mb_convert_case('virá', MB_CASE_UPPER, $encoding);

This is because mb_* functions will operate on strings based on their Unicode properties. Accented characters are not regular "sets" but multibytes. That's why if you use strlen("will come") the result will be 5 characters instead of 4 (as you'd expect).

About chosen encoding

Since we cannot guess in which encoding the files are saved and which encoding is used in the output, it is not possible to inform the correct one here. You should find this out. The best advice is to save the source files in UTF-8 (every editor has this option) and in the output force the output in UTF-8, using the tag <meta charset="SEU-ENCODING">

Scroll to Top