简体   繁体   中英

Check if php string is alphanumeric including Cyrillic characters?

I have found a lot of different patterns on the net (tested them all), i have also added encoding to the page, but nothing seams to work. This is my code:

<?php header('Content-Type: text/html; charset=utf-8'); ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body>
<?php



$teststring="cc12cž";


$pattern  = "/^[p{L}\p{M}\a-zA-Z*0-9\s\-]+$/u";
if(preg_match($pattern, $teststring))
    {echo"IT IS ALFANUMERIC";}
else
    {echo"ERROR";}


?>

</body></html>

This is what i tried

//$pattern  = "/^[\p{L}\p{M}\a-zA-Z*0-9\s\-]+$/u"; 
//$pattern  = "/^[p{L}\p{M}\a-zA-Z*0-9\s\-]+$/u"
//$pattern  ='/^[a-zA-Z\p{Cyrillic}\d\s\-]+$/u'
//$pattern  ="/(*UTF8)^[[:alnum:]]+$/"
//$pattern  ="/^[a-zA-Z\p{Cyrillic}\p{Cyrillic}]+$/u"

I did a test using ZF Zend_Validate_Alnum and your string seems to validate correctly.

$validator = new Zend_Validate_Alnum();
if ($validator->isValid('cc12cž')) {
    // value contains only allowed chars
    echo "IT IS ALFANUMERIC";
} else {
    echo "ERROR";
} 

From what I see their validation technique is quite simple and it does not involve regexp :

if (!is_string($value) && !is_int($value) && !is_float($value)) {
    $this->_error(self::INVALID);
    return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM