Home >Backend Development >PHP Tutorial >PHP implements regular verification of Chinese characters in usernames

PHP implements regular verification of Chinese characters in usernames

小云云
小云云Original
2018-03-27 16:51:323880browse

In PHP, username format verification is performed through regular expressions. This article mainly shares with you how PHP implements regular Chinese character verification for usernames. I hope it can help you.

Username verification rules: Username can only consist of numbers, letters, Chinese characters and underscores, and cannot contain special symbols.

/^[a-zA-Z][\w]{3,14}$|^[\x{4e00}-\x{9fa5}a-z-A-Z_]{1}([\x {4e0}-\x{9fa5}]|[\x{4e00}-\x{9fa5}a-z-A-Z\d_]){3,6}$/u

ps: This is my own composition The regular expression is a bit long and complicated;

The key lies in the paragraph "/^[A-Za-z0-9_\x{4e00}-\x{9fa5}]+$/u", in which " A-Za-z0-9_" represents verification of letters, numbers and underscores,

"\x{4e00}-\x{9fa5}" represents verification of Chinese characters. "/u" represents unicode (utf-8) matching.

In PHP, [\u4e00-\u9fa5] is not supported to match Chinese characters. Instead, \x is used to represent hexadecimal data. However, "[\x4e00-\x9fa5]" is also written in PHP. Wrong,

must be wrapped with {}. In addition, due to the encoding relationship, "/u" needs to be used to declare the encoding format.

Related recommendations:

php Chinese character regular verification

The above is the detailed content of PHP implements regular verification of Chinese characters in usernames. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn