6

I have a php function to validate "City":

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 "";
}

Every time, when I enter a cyrillic City name (for ex: "Минск") it returns: City hame only from letters and -. Variable $_POST['city'] looks like: Ð�инÑ�к

In JS this code works correct, I think something is in encoding.....

1
  • I had similar problem with this one recently, for cities written in Serbian cyrillic (like "Београд, Москва, Каиро"). Check encoding for both pages, one where there is POST form and this one with function.
    – Wh1T3h4Ck5
    Apr 1, 2011 at 7:48

8 Answers 8

14

You can use the following pattern to validate non latin characters:

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

See this post for the full explanation

4

A better solution to match Cyrillic and Common characters would be:

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

This looks like utf-8, if it is, this tip from cebelab on php.net might be helpful:

I noticed that in order to deal with UTF-8 texts, without having to recompile php with the PCRE UTF-8 flag enabled, you can just add the following sequence at the start of your pattern: (*UTF8)

for instance : '#(*UTF8)[[:alnum:]]#' will return TRUE for 'é' where '#[[:alnum:]]#' will return FALSE

Use the builtin special character group :alnum: for this, you will need to reverse your match:

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, ah, forgot utf-8 in regex ; )

5
  • elseif (preg_match("/(*UTF8)[^а-Яa-zA-z-]/", $field)) return "City hame only from letters and -.<br /><br />"; Now even when I enter: "Минск@&%$()@" it says that everything is allright
    – Nick_NY
    Apr 1, 2011 at 7:55
  • The Characters between a and Я might include more than you intend to match, it even might span 80% of the utf-8 range. Try your regex with [[:alnum:]] - it contains all alphanumeric characters and is defined within php / pcre itself.
    – Stephan B
    Apr 1, 2011 at 8:01
  • (preg_match("/(*UTF8)[[:alnum:]]/", $field)) ?
    – Nick_NY
    Apr 1, 2011 at 8:07
  • every time I see such else-if code without brackets, I want to kill the author
    – Fluffy
    Apr 1, 2011 at 8:11
  • does it help when I say i felt bad writing this? ^^
    – Stephan B
    Apr 1, 2011 at 8:16
0

You have to make sure all your files have the same encoding or encode/decode the data in the appropriate places. If you're working with utf-8, check: - that your page is being displayed in the right encoding (Browser -> view -> encoding) - that your files have the right encoding

Your database, if you have one, should also be in the same encoding you choose everywhere else.

0
0

Yes, it's an encoding problem.
Put this in your page:

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

Or this:

<META http-equiv="Content-Type" content='text/html;charset="windows-1251"'>
1
  • I think that problem is not just in META. Consider that his PHP file must be saved as UTF-8. Transferring data between POST variable and PHP script might be a problem for Non-ANSI characters sometimes, specially if one page is saved as ANSI and another one is UTF-8 or something else.
    – Wh1T3h4Ck5
    Apr 1, 2011 at 7:59
0

Check encoding in response headers (FireBug is a great tool). Possibly you have incorrect value in Webserver configuration (for example, AddDefaultCharset in .htaccess file).

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

1
  • Also, [а-Я] isnt correct range, because small russian letters are going after capitals. And dont forget about й,ё,Й and Ё - they have special code numbers.
    – biakaveron
    Apr 1, 2011 at 9:30
0

Variable $_POST['city'] looks like: �ин�к

It's not an UTF-8.... Maybe problemis in $_POST?

1
  • Problem was in my function fix_string, it was ruining my cyrillic texts... function fix_string($string) { if (get_magic_quotes_gpc()) $string = stripslashes($string); return htmlentities ($string); }
    – Nick_NY
    Apr 1, 2011 at 20:38
0

I try to use ^[a-zA-Z\p{Cyrillic}\d\s\-]+$ in https://regex101.com/ and everything is ok.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.