PHP: mb_strtoupper not working

I have a problem with UTF-8 and mb_strtoupper.

mb_internal_encoding('UTF-8');
$guesstitlestring='Le Courrier de Sáint-Hyácinthe';

$encoding=mb_detect_encoding($guesstitlestring);
if ($encoding!=='UTF-8') $guesstitlestring=mb_convert_encoding($guesstitlestring,'UTF-8',$encoding);

echo "DEBUG1 $guesstitlestring\n";
$guesstitlestring=mb_strtoupper($guesstitlestring);
echo "DEBUG2 $guesstitlestring\n";

Result:

DEBUG1 Le Courrier de Sáint-Hyácinthe
DEBUG2 LE COURRIER DE S?INT-HY?CINTHE

I do not understand why this is happening? I try to be as careful as I can with encoding. The string is first set to UTF-8, checked, and can be converted to UTF-8. A nightmare!

UPDATE

So, I realized that this is caused by a combination of inputting arguments through the console and arguments coming back from the console. Thus, they were distorted both along the way and towards the exit. The solution is to not enter any of the arguments in this way or not to output the arguments in this way.

Thank you all for your help in solving this problem!

+5
source share
3 answers

strtoupper()/mb_strtoupper() mb_convert_case(), , , IS UTF-8.

$content = 'Le Courrier de Sáint-Hyácinthe';

mb_internal_encoding('UTF-8');
if(!mb_check_encoding($content, 'UTF-8')
    OR !($content === mb_convert_encoding(mb_convert_encoding($content, 'UTF-32', 'UTF-8' ), 'UTF-8', 'UTF-32'))) {

    $content = mb_convert_encoding($content, 'UTF-8'); 
}

// LE COURRIER DE SÁINT-HYÁCINTHE
echo mb_convert_case($content, MB_CASE_UPPER, "UTF-8"); 

: http://3v4l.org/enEfm#v443

. - PHP : http://www.php.net/manual/function.utf8-encode.php#102382

+5

, , php UTF-8, , , UTF-8. , , ISO-8859-1, ISO-8859-1.

-, mb_detect_encoding . PHP UTF-8, UTF-8.

, ISO-8859-1, . , UTF-8, .

. , ISO-8859-1, iso-8859-1.php. iconv UTF-8 utf-8.php

iconv iso-8859-1.php --from iso-8859-1 --to UTF-8 > utf-8.php

, , mb_detect_encoding.

$ file iso-8859-1.php 
iso-8859-1.php: PHP , ISO-8859 text

$ php iso-8859-1.php 
ENCODING: UTF-8
DEBUG1 Le Courrier de S int-Hy cinthe
DEBUG2 LE COURRIER DE S?INT-HY?CINTHE

$ file utf-8.php 
utf-8.php: PHP , UTF-8 Unicode text

$ php utf-8.php 
ENCODING: UTF-8
DEBUG1 Le Courrier de Sáint-Hyácinthe
DEBUG2 LE COURRIER DE SÁINT-HYÁCINTHE

UTF-8, , ISO-8859-1, . , utf-8, utf-8.

+2

Actually, it just works here

<?php
mb_internal_encoding('UTF-8');

$x='Le Courrier de Sáint-Hyácinthe';
echo mb_strtoupper( $x ) . "\n";

exits

LE COURRIER DE SÁINT-HYÁCINTHE

here it works directly, but maybe in your case you need to add utf8_encode:

$x = utf8_encode( 'Le Courrier de Sáint-Hyácinthe' );

-

An alternative that works here without MB,

<?php
echo strtoupper(str_replace('á', 'Á', 'Le Courrier de Sáint-Hyácinthe'));
+2
source

All Articles