There is an expression

!preg_match( "/^[a-zA-Z0-9]+$/", $string ) 

It is necessary to add support for the special symbol of the dog @ and points to it . , because he does not miss the email, as well as, if possible, the support of the Cyrillic alphabet.

How can I do that?

    3 answers 3

    If only two characters are doggy and period, then so

     preg_match("/^[a-zA-Z0-9@.]+$/", $string) 

    do not need to shield the point inside the square brackets.

    If you need Cyrillic support, then you can

     preg_match("/^[a-zA-Z0-9А-Яа-я]+$/", $string) 

    But most likely it will not work. (all tied to unicode). Then, with a high probability, the checkbox /u will help:

     preg_match("/^[a-zA-Z0-9А-Яа-я]+$/u", $string) 

    But there is one subtlety yet. The string from the browser can come for example in cp1251, and on the server the scripts are in utf-8 or koi. And regulars with Cyrillic will not work (or will, but "unexpectedly" and incorrectly). Recommendation - translate everything into one encoding.

    • and it is possible to convert it to UTF-8 and then convert it back. - jcmax
    • those. It turns out I need to know what is coming from the server and what encoding does the client send? I have UTF-8 installed in my header, the file itself is also saved in UTF-8 - jcmax
    • Yes, you need to know the encoding. But as for me, it is better to keep utf-8 everywhere, it allows you to avoid many problems. For character encodings, use the iconv and sibling functions. - KoVadim
    • How unpatriotic have you forgotten the letter Ё ! The documentation about it remembers: The range of valid Cyrillic characters for domains .РФ includes UTF — 8 characters from the character “a” to the character “i” and the additional character “e”. Therefore, the Russian letters should go block [А-яЁё] . By the way, they also forgot about the hyphen. - Egor Skriptunoff
    • About the hyphenated vehicle did not write anything. But first I wrote, but if you already do support for the Cyrillic alphabet, then you need to write and є, і, ї, :). Cyrillic alphabet is not only Russian. TC did not write anything about Russian, so there’s no need to blame Ukrainians for non-patriotic Russian :) - KoVadim

    If it is not necessary to support Cyrillic, then use native PHP tools to validate the filter_var data:

     if (filter_var($email,FILTER_VALIDATE_EMAIL)) { echo 'valid'; } else { echo 'invalid'; } 

    See the result

    If through regexes, then:

     if (preg_match('/^(\\w+[\\w\.\+\-]+)?\\w+@(\\w+\.)+\\w+$/iu', $email)) { echo 'valid'; } 

    See the result

    • I have a function in the class that filters the data. It must skip the email exactly the characters in the email @ and. as standard characters, I have another function for filtering a name or a filter_vars. - jcmax

    @you add simply as @, and the dot in the form \., since it must be escaped with a backslash.

    • like this? ! preg_match ("/ ^ @ \. [a-zA-Z0-9] + $ /", $ string) Non-working: ( - jcmax
    • So you add the condition that @. must stand at the beginning, if you want to anywhere, add to the brackets, if necessary at the end, add to the end - Yaroslav