开发者

Accept Unicode Characters along with Underscore via Regex

if(preg_match('@^([^\W_]*\s){0,3}[^\W_]*$@', $_POST['txt_username_reg'])) 
. . .

The above line is used in PHP.

'@^([^\W_]*\s){开发者_C百科0,3}[^\W_]*$@'

The regex allows "English characters + numbers + 3 spaces maximum".

How can i allow the following:

  • 4 Underscores maximum & Unicode Characters (Only with No Special Characters Allowed - Discard the request enclosed within brackets).


\W will match against Standard ASCII English characters, I have done research about this before and could never find a solution until now.

I would normally suggest to match against delimiters (or boundaries) of the words instead and forbid any unwanted characters.

However different approaches are available, check these links for more information.

  1. http://www.regular-expressions.info/unicode.html
  2. http://www.regular-expressions.info/refunicode.html
  3. http://www.regular-expressions.info/wordboundaries.html


\p{L} matches any Unicode letter. So

if(preg_match('/^(?:\p{L}*_){1,4}\p{L}*$/u', $_POST['txt_username_reg']))

implements your requirements. However, currently these requirements allow _ as a password, so you might want to rethink those.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜