strtoupper() with accents

Asked

Viewed 19,928 times

20

The function strtoupper() PHP is not turning the accented letters into uppercase, see the example:

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

Do you have any native function that solves this problem?

  • 3

    echo mb_strtoupper("will come",'UTF-8');

4 answers

37


You need to use your counterpart, mb_strtoupper() that will deal with Unicode:

$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’s why if you use strlen("will come") the result will be 5 characters instead of 4 (as you expected).

About chosen encoding

Since we cannot guess in which encoding the files are saved and in which encoding is used in the output, we cannot say 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 in output force the output in UTF-8, using tag <meta charset="SEU-ENCODING">

  • with the meta tag in utf-8 gets "Will" and without the tag only php with echo will get "VIR¡"

  • Oops now yes rolled, thank you!!

  • @Sneepsninja we can’t guess at which encoding you save your files and what encoding you use in the output. You should find out this. The best advice is that you save your source files in UTF-8 (every editor has this option) and in output force the output in UTF-8, using <meta charset="SE-ENCODING">

11

MB_STRTOUPPER

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

Or

mb_internal_encoding('UTF-8');
mb_strtoupper('virá');
  • I tested it and it didn’t work...

  • Which your charset ?

  • It is, lacked to put that information that has to inform the encoding..

  • yes need to pass this encoding, now it worked @felipsmartins updated his response as well

  • Beauty. I didn’t put it first because I tested it here before on a page of mine without going Encoding and it was good.

1

In addition to having to deal with function accentuation strtoupper I also had to make a cast with the data coming from the bank, the code was like this.

mb_strtoupper(utf8_encode($variavel_do_banco['nome_coluna_banco']));
  • Worth as an observation. (soon you can comment on any post, only missing a few points of reputation). Perhaps this is not the most appropriate solution, because it happened because of the bank configured in an incompatible way. I would suggest always working on the same encoding, both in the code editor and in the DB and HTTP service

  • @Bacco about this error in character encoding, I found the solution here

  • 2

    As I said, it is a palliative solution in this context. The Internet and Code are valid to make two different systems compatible, but in one application only, in theory, they should never be used (after all, the application should work with the same encoding in all layers). In other words, in fact what you said may correct the symptoms, but the suggestion is always try to resolve at source.

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');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.