Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setlocale() is not working to uppercase accented characters

I have some strings with (Swiss) French characters that I would like to uppercase (PHP 5.3).

echo strtoupper('société');

As strtoupper() does not work with accuentuated characters, I do a setlocale() (this locale is available on our Ubuntu dev and debian prod servers), it does not work:

setlocale(LC_CTYPE, 'fr_CH');
echo strtoupper('société');

Expected result:

SOCIÉTÉ

Result:

SOCIéTé

Available locales:

$ locale -a
...
fr_CH
fr_CH.iso88591
fr_CH.utf8
fr_FR
fr_FR.iso88591
fr_FR.iso885915@euro
fr_FR.utf8
fr_FR@euro
...

$ locale
LANG=
LANGUAGE=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=

Note: The mbstring module is not available.

like image 614
Toto Avatar asked May 15 '12 11:05

Toto


1 Answers

Use mb_strtoupper() function. mb_strtoupper($str, 'UTF-8');

http://in3.php.net/manual/en/function.mb-strtoupper.php

Info

strtoupper is not an unicode aware. You should use multibyte version of string funcitons

Alternative

<?php function mb_strtoupper_new($str, $e='utf-8') {
    if (function_exists('mb_strtoupper')) {
        return mb_strtoupper($str, $e='utf-8'); 
    }
    else {
        foreach($str as &$char) {
            $char = utf8_decode($char);
            $char = strtr(char,
                "abcdefghýijklmnopqrstuvwxyz".
                "\x9C\x9A\xE0\xE1\xE2\xE3".
                "\xE4\xE5\xE6\xE7\xE8\xE9".
                "\xEA\xEB\xEC\xED\xEE\xEF".
                "\xF0\xF1\xF2\xF3\xF4\xF5".
                "\xF6\xF8\xF9\xFA\xFB\xFC".
                "\xFE\xFF",
                "ABCDEFGHÝIJKLMNOPQRSTUVWXYZ".
                "\x8C\x8A\xC0\xC1\xC2\xC3\xC4".
                "\xC5\xC6\xC7\xC8\xC9\xCA\xCB".
                "\xCC\xCD\xCE\xCF\xD0\xD1\xD2".
                "\xD3\xD4\xD5\xD6\xD8\xD9\xDA".
                "\xDB\xDC\xDE\x9F");
            $char =  utf8_encode($char);
        }
        return $str;
    }
}
echo mb_strtoupper_new('société');

source: http://www.php.net/manual/es/function.ucfirst.php#63799

like image 184
Venu Avatar answered Nov 15 '22 13:11

Venu