我运行这个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会接受像äöüß这样的德语字符,如文档中所述:"Returns TRUE if every character in text is a letter from the current locale, FALSE otherwise."但它没有。 我在这做错了什么?
PHP版本是:5.3.10-1ubuntu3.8
答案 0 :(得分:0)
没有记录,但是,由于您使用的是utf8
版本的语言环境,如果您的文件编码也是UTF-8,则必须使用utf8_decode()
才能使其正常工作:
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)
使用UTF-8,ISO-8859-1,KOI8-U和Windows 1252文件编码在OS X Mavericks机器上使用PHP 5.4.17-cli进行测试。