It can be done with preg_match() with the following pattern:
$str="ABC abc 1234 АБВ абв";
$pattern = "/^[a-zA-Z\p{Cyrillic}0-9\s\-]+$/u";
$result = (bool) preg_match($pattern, $str);
if($result)
echo "$str is composed of Cyrillic and alphanumeric characters\n";
Here is a decomposition of the pattern:
a-zA-Z is for the Latin characters. You can omit this if you want only Cyrillic input.
\p{Cyrillic} is for the cyrillic characters. You can also use \p{Arabic}, \p{Greek} or other alphabeths. See this site for a full list of utf8 scripts.
0-9 is for the numbers.
\s is for the space
\- is for the dash
Nice solution, but what if I wanted to validate only UPPERCASE Cyrillic characters?
ReplyDelete/^[А-Я]+$/u might be what you are looking for.
ReplyDeleteWhat if I want to strip all non alphanumeric characters but also allow all foriegn characters? So utf8 safe.
ReplyDeleteitworkarounds.blogspot.ru:80/2011/08/validating-cyrillic-utf8-alphanumeric.html
ReplyDelete