Tuesday, January 24, 2012

Method preg_match() for Cyrillic characters in PHP

Sometimes I encounter the problem with preg_match() method, because it doesn't treat Cyrillic characters the proper way. The problem also corresponds to other non-latin characters.
There is a solution for the search in the string that contains non-latin characters.
<?php 
  preg_match("/^[a-zA-Z\p{Cyrillic}]+$/u", "AбВгд");
?>
Do not forget about the Pattern Modifier - u
u (PCRE_UTF8)

This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern strings are treated as UTF-8.

This modifier is available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32. UTF-8 validity of the pattern is checked since PHP 4.3.5.