strtoupper () with accents

16

The strtoupper() function of PHP is not transforming the letters with a capital accent, see example:

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

Do you have a native function that solves this problem?

    
asked by anonymous 03.09.2015 / 14:10

4 answers

26

You need to use your counterpart, mb_strtoupper () that will handle unicode: / p>

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

or

$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 "formations", but multibytes. That is why if you use strlen ("will") the result will be 5 characters, instead of 4 (as you expected).

About encoding chosen

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

    
03.09.2015 / 14:12
8

MB_STRTOUPPER

mb_strtoupper('virá', 'UTF-8');

Or

mb_internal_encoding('UTF-8');
mb_strtoupper('virá');
    
03.09.2015 / 14:11
0

In addition to having to deal with accentuation of the strtoupper function, I also had to cast cast with the data coming from the database,

mb_strtoupper(utf8_encode($variavel_do_banco['nome_coluna_banco']));
    
27.08.2016 / 14:25
0

If you use ISO-8859-1

header ('Content-type: text/html; charset=ISO-8859-1');

echo mb_strtoupper("maçãs são boas", 'ISO-8859-1');
    
14.05.2018 / 15:09