2012-04-04 96 views
2

我有PHP函数CTYPE_ALNUM这个奇怪的问题奇怪的事情有了CTYPE_ALNUM

,如果我这样做:

PHP:

$words="àòè"; 


if(ctype_alnum($words)){ 

    Echo "Don't work"; 

}else{ 

    Echo "Work";  

} 

这呼应了 '工作'

BUT如果我有一个表格,并以这种形式插入字母与坟墓像(à,è,ò),这将回声'不工作'

代码:

<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";  

} 

} 

如果我插入的文本输入或E或ò这将回声出字母“不工作”

+0

什么是文件编码? – zerkms 2012-04-04 11:48:33

+0

'print_r($ words)'显示什么? – MichaelRushton 2012-04-04 12:00:58

+0

它显示了我写的字母,如果我写èèè它显示èèè – LdB 2012-04-04 12:14:43

回答

4

ctype_alnum是语言环境的依赖性。这意味着如果您使用的是标准C语言环境或普通类似en_US,则不会与重音字母匹配,只能使用[A-Za-z]。你可以尝试设置的语言环境到通过setlocale表彰那些推导语言(注意,该区域需要在您的系统上安装,而不是所有的系统都是一样的),或者使用更便携的解决方案,如:

function ctype_alnum_portable($text) { 
    return (preg_match('~^[0-9a-z]*$~iu', $text) > 0); 
} 
+0

好吧,我会做一些尝试,如果是很奇怪XD – LdB 2012-04-04 12:27:09

+0

Alix,我相信你正在使用的正则表达式并不完美。 preg_math('〜^ [0-9a-z] * $〜iu',“LajosÁrpád”)将返回0。 – 2015-11-18 14:06:36

0

如果您想检查Unicode标准中定义的所有字符,请尝试以下代码。我在Mac OSX中遇到了错误的检测。

//setlocale(LC_ALL, 'C'); 
setlocale(LC_ALL, 'de_DE.UTF-8'); 

for ($i = 0; $i < 0x110000; ++$i) { 

    $c = utf8_chr($i); 
    $number = dechex($i); 
    $length = strlen($number); 

    if ($i < 0x10000) { 
     $number = str_repeat('0', 4 - $length).$number; 
    } 

    if (ctype_alnum($c)) { 
     echo 'U+'.$number.' '.$c.PHP_EOL; 
    } 

} 
function utf8_chr($code_point) { 

    if ($code_point < 0 || 0x10FFFF < $code_point || (0xD800 <= $code_point && $code_point <= 0xDFFF)) { 
     return ''; 
    } 

    if ($code_point < 0x80) { 
     $hex[0] = $code_point; 
     $ret = chr($hex[0]); 
    } else if ($code_point < 0x800) { 
     $hex[0] = 0x1C0 | $code_point >> 6; 
     $hex[1] = 0x80 | $code_point & 0x3F; 
     $ret = chr($hex[0]).chr($hex[1]); 
    } else if ($code_point < 0x10000) { 
     $hex[0] = 0xE0 | $code_point >> 12; 
     $hex[1] = 0x80 | $code_point >> 6 & 0x3F; 
     $hex[2] = 0x80 | $code_point & 0x3F; 
     $ret = chr($hex[0]).chr($hex[1]).chr($hex[2]); 
    } else { 
     $hex[0] = 0xF0 | $code_point >> 18; 
     $hex[1] = 0x80 | $code_point >> 12 & 0x3F; 
     $hex[2] = 0x80 | $code_point >> 6 & 0x3F; 
     $hex[3] = 0x80 | $code_point & 0x3F; 
     $ret = chr($hex[0]).chr($hex[1]).chr($hex[2]).chr($hex[3]); 
    } 

    return $ret; 
}