Yii Framework Forum

Regex for Cyrillic characters


(Mh) #1

As we have a lot of russian members here, maybe someone can help: I couldn’t find much information wether it’s possible to check for cyrillic characters (А-Я) with a regular exp​ression in PHP. Any hints are highly appreciated!

Thanks!


(Alexander Makarov) #2

Should be А-Яа-я


(Mh) #3

Thanks. Did you verify this? I thought there are some problems with UTF-8 + regex. A comment in the PHP manual says, that PCRE functions aren’t UTF-8 safe. It states that you can only use mb_ereg* functions and need to alter this:


<?php

  mb_internal_encoding('UTF-8');

  mb_regex_encoding('UTF-8');

  setlocale(LC_ALL, 'fr-fr');


(Alexander Makarov) #4

I’m talking about preg_ with unicode modifier, not ereg.


(Mh) #5

Ah, thanks a lot. Didn’t know that one.


(Webshark) #6

Maybe this will be useful as well…

Regards.


(Koredalin) #7

I had the same issue, so I have searched too.

One option: "/^[А-Я]+$/u".

A successful cyrillic, latin alphanumeric validation for Yii2…


public function rules()

{

    return [

['name', 'match', 'pattern' =&gt; '/^[А-Яа-яA-Za-z0-9\s_-]+$/u', 'message' =&gt; 'Your username can only contain alphanumeric characters, underscores and dashes.'],

    ];

}

If you need russian characters… - ‘/[А-Яа-яЁё]/u’ - This may help.