strtoupper PHP function for UTF-8 string

南楼画角 提交于 2019-11-30 07:46:39

问题


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?

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?


回答1:


You want to use mb_strtoupper.

$str = "Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός";
$str = mb_strtoupper($str, "UTF-8");
echo $str; // Prints ΤΆΧΙΣΤΗ ΑΛΏΠΗΞ ΒΑΦΉΣ ΨΗΜΈΝΗ ΓΗ, ΔΡΑΣΚΕΛΊΖΕΙ ΥΠΈΡ ΝΩΘΡΟΎ ΚΥΝΌΣ

PHP.net states:

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 (Ä).




回答2:


Have you tried this? I'm no linguist, but I wouldn't assume that all languages have a lower / upper case split.



来源:https://stackoverflow.com/questions/5969803/strtoupper-php-function-for-utf-8-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!