PHP setlocale()不工作ctype_alpha检查 [英] PHP setlocale() not working for ctype_alpha check
问题描述
我运行此php代码:
echo "<br>system locales: ".system('locale -a')."<br><br>";
echo "current locales: ".setlocale(LC_ALL, 0)."<br><br>";
var_dump(setlocale (LC_ALL, 'de_DE.utf8'));
echo "current locales: ".setlocale(LC_ALL, 0)."<br><br>";
echo "accepting german characters?: ".ctype_alpha("äüöß")."<br><br>";
echo "accepting characters in general?: ".ctype_alpha("test")."<br><br>";
echo "rejecting numbers?: ".ctype_alpha("tes2t")."<br><br>";
并获得此输出:
C C.UTF-8 POSIX de_DE.utf8
system locales: de_DE.utf8
current locales: C
string(10) "de_DE.utf8" current locales: de_DE.utf8
accepting german characters?:
accepting characters in general?: 1
rejecting numbers?
我希望在调用setlocale(LC_ALL,'de_DE.utf8')后ctype_alpha将接受德语字符如äöüß,如文档中所述:如果每个字符文字是来自当前语言区域的字母,否则为FALSE。,但不是。
我在这里做错了什么?
I expected that after the call to setlocale (LC_ALL, 'de_DE.utf8') ctype_alpha would accept german characters like äöüß, as written in the documentation: "Returns TRUE if every character in text is a letter from the current locale, FALSE otherwise." but it doesn't. What am i doing wrong here?
PHP版本是:5.3.10-1ubuntu3.8
PHP Version is : 5.3.10-1ubuntu3.8
推荐答案
它不是文件,但是,由于你使用 utf8
版本的语言环境,如果你的文件编码也是UTF-必须使用 utf8_decode()
才能使其工作:
It's not documented but, since you are using utf8
version of the locale, if your file encoding is also UTF-8 you'll have to use utf8_decode()
to make it work:
setlocale(LC_ALL, 'de_DE.utf8');
// Assuming your file encoding is also UTF-8
var_dump(ctype_alpha(utf8_decode('äüöß'))); // bool(true)
// Assuming your file encoding is anything else
var_dump(ctype_alpha('äüöß')); // bool(true)
在OS X Mavericks机器上使用PHP 5.4.17-cli测试UTF-8,ISO-8859-1,KOI8-U和Windows 1252文件编码。
Tested with PHP 5.4.17-cli on a OS X Mavericks machine with UTF-8, ISO-8859-1, KOI8-U and Windows 1252 file encodings.
这篇关于PHP setlocale()不工作ctype_alpha检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!