2012-04-04 74 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; 
}