Regular expression to check cyrillic

I have a php function to check for "Cities":

function validate_city($field) {
    if ($field == "") return "Enter city.<br />";
    else if (preg_match("/[^-a-zA-z-]/", $field))
        return "City hame only from letters and -.<br />";
    return "";
}

Each time I enter the name of the Cyrillic city (for example, "Minsk"), it returns: City hame only from letters and -. The variable $ _POST ['city'] looks like this:

In JS, this code works correctly, I think something is encoded .....

+5
source share
7 answers

You can use the following pattern to check non-latin characters:

preg_match ('/^[a-zA-Z\p{Cyrillic}\d\s\-]+$/u', $str);

See this post for a full explanation.

+13
source

It looks like utf-8, if it is, this advice from cebelab on php.net may be helpful:

, UTF-8, php PCRE UTF-8, : (* UTF8)

: '# (* UTF8) [[: alnum:]] #' TRUE 'é', '# [[: alnum:]] #' FALSE

: alnum: :

function validate_city($field) {
    if ($field == "") return "Enter city.<br />";
    else if (preg_match("/(*UTF8)^[[:alnum:]]+$/", $field))
    return "";
    return "City hame only from letters and -.<br />";
} 

edit, , utf-8 regex; )

+2

:

preg_match ('/^[\p{Cyrillic}\p{Common}]+$/u', $str);
+2

, .
:

<META http-equiv="Content-Type" content='text/html;charset="UTF-8"'>

:

<META http-equiv="Content-Type" content='text/html;charset="windows-1251"'>
0

, / . utf-8, : - (Browser → view → encoding) -

, , , .

0

Check the encoding in the response headers (FireBug is a great tool). Perhaps you have the wrong value in the Webserver configuration (for example, AddDefaultCharsetin the .htaccess file).

PS. Use UTF regular expressions instead of character ranges ( preg_match("/[^\pL-]/u", $field))

0
source

The variable is $_POST['city']as follows:Ð Ð¸Ð½Ñ Ðº

This is not UTF-8.... Maybe the problem is $_POST?

0
source

All Articles