2011-08-04 23 views
6

是否有一種方法(正則表達式?)檢查字符串是否僅由Cyrilic字母數字字符組成?在PHP中驗證Cyrilic輸入

我需要驗證的輸入是在西裏爾字母的範圍,再加上數字,破折號和空格

回答

6

\p{Cyrillic}比賽西里爾字符(你可以使用阿拉伯語希臘等其他字母)

\d匹配號碼

\s空白字符匹配

\-比賽破折號

<?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

這不正是我需要的。感謝Nabil –