开发者

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 inp开发者_运维问答ut to be in range of the Cyrillic alphabet, plus numbers, dashes and spaces


\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 />";
    }
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜