Strange Thing With Ctype_alnum
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'; }
Solution 1:
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:
functionctype_alnum_portable($text) {
return (preg_match('~^[0-9a-z]*$~iu', $text) > 0);
}
Solution 2:
If you want to check all characters defined in the Unicode Standard, try the following code. I met false detection in 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]);
} elseif ($code_point < 0x800) {
$hex[0] = 0x1C0 | $code_point >> 6;
$hex[1] = 0x80 | $code_point & 0x3F;
$ret = chr($hex[0]).chr($hex[1]);
} elseif ($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;
}
Post a Comment for "Strange Thing With Ctype_alnum"