php php正则验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3747362/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:58:05  来源:igfitidea点击:

php regex validation

phpregex

提问by Elliott

just a quick question am abit rubish with regex so thought I would post on here. The regex below is to validate a username.

只是一个简单的问题对正则表达式有点垃圾,所以我想我会在这里发帖。下面的正则表达式用于验证用户名。

  • Must be between 4-26 characters long

  • Start with atleast 2 letters

  • Can only contain numbers and one underscore and one dot
  • 长度必须在 4-26 个字符之间

  • 以至少 2 个字母开头

  • 只能包含数字和一个下划线和一个点

I have this so far, but isn't working

到目前为止我有这个,但没有用

<?php

$username=$_POST['username'];

if (!eregi("^([a-zA-Z][0-9_.]){4,26}$",$username))
{
 return false;
}
else
{
 echo "username ok";
}

?>

Thanks :)

谢谢 :)

回答by kennytm

You could use the regex

你可以使用正则表达式

/^(?=[a-z]{2})(?=.{4,26})(?=[^.]*\.?[^.]*$)(?=[^_]*_?[^_]*$)[\w.]+$/iD

as in

<?php

$username=$_POST['username'];

if (!preg_match('/^(?=[a-z]{2})(?=.{4,26})(?=[^.]*\.?[^.]*$)(?=[^_]*_?[^_]*$)[\w.]+$/iD',
                $username))
{
 return false;
}
else
{
 echo "username ok";
}

?>
  • The ^(?=[a-z]{2})ensure the string "Start with atleast 2 letters".
  • The (?=.{4,26})ensure it "Must be between 4-26 characters long".
  • The (?=[^.]*\.?[^.]*$)ensures the following characters contains at most one .until the end.
  • Similarly (?=[^_]*_?[^_]*$)ensures at most one _.
  • The [\w.]+$commits the match. It also ensures only alphanumerics, _and .will be involved.
  • ^(?=[a-z]{2})确保字符串“开始至少2个字母”。
  • (?=.{4,26})确保“必须是4-26个字符之间”。
  • (?=[^.]*\.?[^.]*$)确保以下字符最多包含一个.才结束。
  • 同样(?=[^_]*_?[^_]*$)确保最多一个_.
  • [\w.]+$承诺的比赛。它还确保只有字母数字,_并且.将涉及。

(Note: this regex assumes hello_worldis a valid user name.)

(注意:这个正则表达式假定hello_world是一个有效的用户名。)

回答by Jim W.

You are asking for a lot in one regex, this may be possible but it is not the best way, you want to let the user know why their username is erroring out.

您在一个正则表达式中要求很多,这可能是可能的,但这不是最好的方法,您想让用户知道为什么他们的用户名出错。

function checkUsername($username) {
    $username = trim($username);
    if (empty($username)) {
        return "username was left blank.";
    }elseif (strlen($username) < 4) {
        return "username was too short";
    }elseif (strlen($username) > 26) {
        return "username was too long";
    }elseif (!preg_match('~^[a-z]{2}~i', $username)) {
        return "username must start with two letters";
    }elseif (preg_match('~[^a-z0-9_.]+~i', $username)) {
        return "username contains invalid characters.";
    }elseif (substr_count($username, ".") > 1) {
        return "username may only contain one or less periods.";
    }elseif (substr_count($username, "_") > 1) {
        return "username may only contain one or less underscores.";
    }

    return true;
} 

Then to check you would do:

然后检查你会做:

$validUsername = checkUsername($username);

if ($validusername !== true) {
     echo "An error occured: " . $validUsername;
}

This way the user knows there was an error and can fix it easier instead of being blind to what the error is. Also you should stop using eregit has been depreciated use preg_matchinstead.

通过这种方式,用户知道存在错误并且可以更容易地修复它,而不是对错误是什么视而不见。此外,您应该停止使用ereg它已折旧使用preg_match代替。

NOTEthe use of the !== to check, this also checks type since it can return a string (which would return true with a loose comparison). Just an FYI.

注意使用 !== 来检查,这也检查类型,因为它可以返回一个字符串(它会在松散比较的情况下返回 true)。仅供参考。

回答by Kevin Sedgley

The part where you said:

你说的那部分:

and one underscore and one dot

和一个下划线和一个点

would steer me away from regular expressions to be honest!

老实说,会让我远离正则表达式!

I would do something like:

我会做这样的事情:

if(!eregi('^[a-zA-Z]{2}$', substr(0,2))) {
    // .... check for only 2 dots here later ....
}