php 将用户名验证为带下划线的字母数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1330693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validate username as alphanumeric with underscores
提问by Gumbo
On my registration page I need to validate the usernames as alphanumeric only, but also with optional underscores. I've come up with this:
在我的注册页面上,我需要仅将用户名验证为字母数字,还需要使用可选的下划线。我想出了这个:
function validate_alphanumeric_underscore($str)
{
return preg_match('/^\w+$/',$str);
}
Which seems to work okay, but I'm not a regex expert! Does anyone spot any problem?
这似乎工作正常,但我不是正则表达式专家!有没有人发现任何问题?
回答by Gumbo
The actual matched characters of \wdepend on the localethat is being used:
A "word" character is any letter or digit or the underscore character, that is, any character which can be part of a Perl "word". The definition of letters and digits is controlled by PCRE's character tables, and may vary if locale-specific matching is taking place. For example, in the "fr" (French) locale, some character codes greater than 128 are used for accented letters, and these are matched by \w.
“单词”字符是任何字母或数字或下划线字符,即可以是 Perl“单词”一部分的任何字符。字母和数字的定义由 PCRE 的字符表控制,如果发生特定于语言环境的匹配,则可能会有所不同。例如,在“fr”(法语)语言环境中,一些大于 128 的字符代码用于重音字母,这些字符代码与 \w 匹配。
So you should better explicitly specify what characters you want to allow:
因此,您最好明确指定要允许的字符:
/^[A-Za-z0-9_]+$/
This allows just alphanumeric characters and the underscore.
这仅允许字母数字字符和下划线。
And if you want to allow underscore only as concatenation character and want to force that the username must start with a alphabet character:
如果您只想允许下划线作为连接字符并想强制用户名必须以字母字符开头:
/^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$/
回答by ifreelancer.asia
Here's a custom function to validate the string by using the PHP ctype_alnumin conjunction with an array of allowed chars:
这是一个自定义函数,通过使用PHP ctype_alnum和允许的字符数组来验证字符串:
<?php
$str = "";
function validate_username($str) {
// each array entry is an special char allowed
// besides the ones from ctype_alnum
$allowed = array(".", "-", "_");
if ( ctype_alnum( str_replace($allowed, '', $str ) ) ) {
return $str;
} else {
$str = "Invalid Username";
return $str;
}
}
?>
回答by Phill Pafford
try
尝试
function validate_alphanumeric_underscore($str)
{
return preg_match('/^[a-zA-Z0-9_]+$/',$str);
}
回答by Lucas Oman
Looks fine to me. Note that you make no requirement for the placement of the underscore, so "username_" and "___username" would both pass.
对我来说看起来不错。请注意,您不需要放置下划线,因此“username_”和“___username”都会通过。
回答by André Catita
I would take gumbo's secondary regex, to only allow underscore as concatenation, but add a + after the _ so a user can be like "special__username", just a minor tweak.
我会采用秋葵汤的辅助正则表达式,只允许下划线作为连接,但在 _ 后添加一个 + 以便用户可以像“special__username”,只是一个小调整。
/^[A-Za-z][A-Za-z0-9]*(?:_+[A-Za-z0-9]+)*$/
/^[A-Za-z][A-Za-z0-9]*(?:_+[A-Za-z0-9]+)*$/

