UTF-8 字符串的 strtoupper PHP 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5969803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
strtoupper PHP function for UTF-8 string
提问by ahmd0
I've been reading user comments for the strtoupper() PHP function and there doesn't seem to a consensus on how to do the conversion for non-Enlgish strings. I mean people offer localized solutions and stuff, but shouldn't there be a uniform way to convert a string to all upper (or all lower) case letters?
我一直在阅读用户对 strtoupper() PHP 函数的评论,但似乎没有就如何对非英语字符串进行转换达成共识。我的意思是人们提供本地化的解决方案和东西,但不应该有一种统一的方法将字符串转换为所有大写(或全部小写)字母吗?
So my question is, say, if I have a UTF-8 encoded string (in some unknown locale) how do I convert it to all upper/lower letters in PHP?
所以我的问题是,如果我有一个 UTF-8 编码的字符串(在一些未知的语言环境中),我如何将它转换为 PHP 中的所有大写/小写字母?
回答by Kelly
You want to use mb_strtoupper
.
您想使用mb_strtoupper
.
$str = "Τ?χιστη αλ?πηξ βαφ?? ψημ?νη γη, δρασκελ?ζει υπ?ρ νωθρο? κυν??";
$str = mb_strtoupper($str, "UTF-8");
echo $str; // Prints Τ?ΧΙΣΤΗ ΑΛ?ΠΗΞ ΒΑΦ?Σ ΨΗΜ?ΝΗ ΓΗ, ΔΡΑΣΚΕΛ?ΖΕΙ ΥΠ?Ρ ΝΩΘΡΟ? ΚΥΝ?Σ
PHP.netstates:
PHP.net指出:
By contrast to the standard case folding functions such as strtolower() and strtoupper(), case folding is performed on the basis of the Unicode character properties. Thus the behaviour of this function is not affected by locale settings and it can convert any characters that have 'alphabetic' property, such as A-umlaut (?).
与 strtolower() 和 strtoupper() 等标准大小写折叠函数相比,大小写折叠是基于 Unicode 字符属性执行的。因此,此函数的行为不受区域设置的影响,它可以转换任何具有“字母”属性的字符,例如变音 (?)。