5

我有一些带有(瑞士)法语字符的字符串,我想大写(PHP 5.3)。

echo strtoupper('société');

由于strtoupper()不适用于accuentuated字符,我做了一个setlocale()(这个语言环境在我们的Ubuntu dev和debian prod服务器上可用),它不起作用:

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

预期结果:

SOCIÉTÉ

结果:

SOCIéTé

可用的语言环境:

$ 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=

注意:该mbstring模块不可用。

4

1 回答 1

6

使用 mb_strtoupper() 函数。mb_strtoupper($str, 'UTF-8');

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

信息

strtoupper 不支持 Unicode。您应该使用多字节版本的字符串函数

选择

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

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

于 2012-05-15T11:42:30.627 回答