Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if php string is alphanumeric including Cyrillic characters?

Tags:

php

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"
like image 259
Dime Avatar asked Sep 01 '26 13:09

Dime


1 Answers

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;
}
like image 171
Elzo Valugi Avatar answered Sep 04 '26 03:09

Elzo Valugi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!