Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating Cyrillic input in PHP

Is there a way (regular expression?) to check if a string is composed only of Cyrillic alphanumeric characters?

I need to validate an input to be in range of the Cyrillic alphabet, plus numbers, dashes and spaces

like image 297
Martin Taleski Avatar asked Aug 04 '11 13:08

Martin Taleski


1 Answers

\p{Cyrillic} matches Cyrillic characters (you can use Arabic, Greek, etc. for other alphabets)

\d matches numbers

\s matches whitespace characters

\- matches dashes

<?php
    header('Content-Type: text/html; charset=utf-8'); 
    $pattern  = "/^[\p{Cyrillic}\d\s\-]+$/u";
    $subjects = array(12, "ab", "АБ", '--', '__');

    foreach($subjects as $subject){
        $match = (bool) preg_match($pattern, $subject);
        if($match)  
            echo "$subject matches the testing pattern<br />";
        else
            echo "$subject does not match the testing pattern<br />";
    }
?>
like image 152
Nabil Kadimi Avatar answered Nov 01 '22 14:11

Nabil Kadimi