奇怪的事情与CTYPE_ALNUM [英] Strange thing With CTYPE_ALNUM
问题描述
我有这个PHP函数CTYPE_ALNUM的奇怪问题如果我这样做的话:
$ b
PHP: / p>
$ words =àòè;
if(ctype_alnum($ words)){
回声不要工作;
} else {
回声工作;
}
这会回应'Work'
但是如果我有一个表格,并且以这种形式插入带有坟墓的字母(à,è,ò),这会回应出'Do not Work'。
代码:
< form action =method =post>
< input type =textname =words/>
< input type =submit/>
< / form>
$ words = $ _ POST ['words'];
if(isset($ words)){
if(ctype_alnum($ words)){
回声不工作;
} else {
回声工作;
}
}
如果我插入输入文字输入字母à或è或ò这会回显出'Do not Work'。
ctype_alnum
是locale-dependend。这意味着如果您使用标准的 C
语言环境或者像 en_US
这样的常用语言环境,字母,只有 [A-Za-z]
。您可以尝试将语言环境设置为可通过 setlocale
(注意区域设置需要安装在您的系统上,而不是所有的系统都一样),或者使用更便携的解决方案,如:
函数ctype_alnum_portable($ text){
return(preg_match('〜^ [0-9a-z] * $〜iu',$ text) > 0);
}
i have this strange problem with the PHP function CTYPE_ALNUM
if i do:
PHP:
$words="àòè";
if(ctype_alnum($words)){
Echo "Don't work";
}else{
Echo "Work";
}
this will echo out 'Work'
BUT if i have a form and in that form i insert the letters with the grave like (à , è, ò) this will echo out 'Don't Work'
Code:
<form action="" method="post">
<input type="text" name="words" />
<input type="submit" />
</form>
$words=$_POST['words'];
if(isset($words)){
if(ctype_alnum($words)){
Echo "Don't Work";
}else{
Echo "Work";
}
}
If i insert into the text input the letters à or è or ò This will echo out 'Don't Work'
ctype_alnum
is locale-dependend. That means if you're using the standard C
locale or a common one like en_US
, that won't match accented letters, only [A-Za-z]
. You can try setting the locale to a language that recognizes those derivations via setlocale
(beware that the locale needs to be installed on your system, and not all systems are alike), or use a more portable solution like:
function ctype_alnum_portable($text) {
return (preg_match('~^[0-9a-z]*$~iu', $text) > 0);
}
这篇关于奇怪的事情与CTYPE_ALNUM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!