PHP的setlocale()不工作而ctype_alpha检查(PHP setlocale() n

2019-10-18 23:26发布

我运行这个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会接受德国的字符,如äöüß,书面文档中: “返回true如果在文本每个字符是从当前区域设置了一封信,否则返回FALSE。 “ 但事实并非如此。 我到底做错了什么?

PHP版本是:5.3.10-1ubuntu3.8

Answer 1:

它没有记录,但由于使用的是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)

用PHP进行测试5.4.17-CLI一个OS X小牛的机器上使用UTF-8,ISO-8859-1,KOI8-U和Windows 1252文件编码。



文章来源: PHP setlocale() not working for ctype_alpha check